[LC++]virtual protected?
Mark Phillips
mark at austrics.com.au
Fri Dec 21 15:42:28 UTC 2001
Hi Roberto,
> I mean I woul like to have a method which could be overided in the derived
> classes but not exposed in the objects..
What you want is the "protected:" interface.
> If I do:
>
> class base
> {
> protected:
> virtual void doSomething ();
> };
>
> The compiler complains..
What does it complain about?
The following code works fine for me (using g++). I think it
is what you are trying to do.
#include <iostream>
using namespace std;
class baseTy {
private:
int dataVt;
protected:
virtual int f() const {
return dataVt;
}
public:
baseTy(int dataAg=0): dataVt(dataAg) {}
virtual ~baseTy() {}
};
class subTy: public baseTy {
protected:
virtual int f() const {
return 2*baseTy::f();
}
public:
int g() const {
return f()*f();
}
subTy(int dataAg): baseTy(dataAg) {}
};
int main(int argc, char* argv[]) {
subTy sub(3);
cout<<"if data is 3, sub.g() is "<<sub.g()<<endl;
}
Cheers,
Mark.
More information about the tuxCPProgramming
mailing list