S
Samuel Burri
Hi there
I got a simple problem. As you can see in the posted source, I have two
classes, where one is derived from the other. In fact, they also contain
virtual functions. In the function doSomething of Derived I'd like to
access private members of Base from the pointer I receive. The compiler
(gcc 4.0) tells me, that Base::foo is protected and it can't access
although Derived itself has access to the specified element.
Probably its best, if you look at the code below, to understand what I
want to do. My questions are 1. What says the standard? 2. If its not a
bug of the compiler how can I do better?
Any comments are appreciated.
Sam
class Base {
protected:
int foo;
public:
Base() : foo(0) {}
};
class Derived : public Base {
public:
void doSomething( Base* base );
};
void Derived::doSomething( Base* base ) {
base.foo = 10;
}
int main() {
Base b;
Derived d;
d.doSomething( &b );
return 0;
}
I got a simple problem. As you can see in the posted source, I have two
classes, where one is derived from the other. In fact, they also contain
virtual functions. In the function doSomething of Derived I'd like to
access private members of Base from the pointer I receive. The compiler
(gcc 4.0) tells me, that Base::foo is protected and it can't access
although Derived itself has access to the specified element.
Probably its best, if you look at the code below, to understand what I
want to do. My questions are 1. What says the standard? 2. If its not a
bug of the compiler how can I do better?
Any comments are appreciated.
Sam
class Base {
protected:
int foo;
public:
Base() : foo(0) {}
};
class Derived : public Base {
public:
void doSomething( Base* base );
};
void Derived::doSomething( Base* base ) {
base.foo = 10;
}
int main() {
Base b;
Derived d;
d.doSomething( &b );
return 0;
}