N
NT
Hi there!
I am puzzled by what I think must be a subtlety of C++ when
list-initializing member variables in derived classes...
Here's what I mean...
class A { // warning l337proggy alert! ;-)
public:
A(int v=9812); // default value provided since this base constructor
// is called by derived classes' constructors
~A();
int getValue();
public:
int value; // it's public, but I don't care!
};
A::A(int v) : value(v) {
cout<<"A() , "<<value<<endl;
}
A::~A() {
cout<<"~A()"<<endl;
}
int A::getValue() {
return value;
}
class B : public A {
public:
B(int v=1);
~B();
};
B::B(int v) {
cout<<"B() , "<<value<<endl; // value is still 9812
}
B::~B() {
cout<<"~B()"<<endl;
}
class C : public B {
public:
C(int v);
~C();
int setValue(int v);
};
C::C(int v) {
cout<<"C() , "<<value<<endl; // 9812 because of A::A()
value=v;
cout<<" "<<value<<endl;
}
C::~C() {
cout<<"~C()"<<endl;
}
int C::setValue(int v) {
value=v;
}
int main(void) {
B *b = new B(5);
C *c = new C(10);
cout<<b->getValue()<<endl;
cout<<c->getValue()<<endl;
c->setValue(32);
cout<<c->getValue()<<endl;
delete b;
delete c;
return 0;
}
Well, so far, so good... But, something weird's happening when I change
C::C() and use an initialization list instead of an assignment in the
body...
C::C(int v) : value(v) {
cout<<"C() , "<<value<<endl;
}
GCC (version 3.3) is really not happy with that!:
proggy.cpp:41: error: class `C' does not have any field named `value'
Ok, _now_ I'm confused! I thought derived classes had access to all
non-private data of their base class?
I guess there is something absolutely trivial that I'm missing here...
Yeah, hard to believe I'm been using C++ for years, isn'it? (albeit on a
_very_ on and -mostly- off basis...)
Is there someone to help?
NT
I am puzzled by what I think must be a subtlety of C++ when
list-initializing member variables in derived classes...
Here's what I mean...
class A { // warning l337proggy alert! ;-)
public:
A(int v=9812); // default value provided since this base constructor
// is called by derived classes' constructors
~A();
int getValue();
public:
int value; // it's public, but I don't care!
};
A::A(int v) : value(v) {
cout<<"A() , "<<value<<endl;
}
A::~A() {
cout<<"~A()"<<endl;
}
int A::getValue() {
return value;
}
class B : public A {
public:
B(int v=1);
~B();
};
B::B(int v) {
cout<<"B() , "<<value<<endl; // value is still 9812
}
B::~B() {
cout<<"~B()"<<endl;
}
class C : public B {
public:
C(int v);
~C();
int setValue(int v);
};
C::C(int v) {
cout<<"C() , "<<value<<endl; // 9812 because of A::A()
value=v;
cout<<" "<<value<<endl;
}
C::~C() {
cout<<"~C()"<<endl;
}
int C::setValue(int v) {
value=v;
}
int main(void) {
B *b = new B(5);
C *c = new C(10);
cout<<b->getValue()<<endl;
cout<<c->getValue()<<endl;
c->setValue(32);
cout<<c->getValue()<<endl;
delete b;
delete c;
return 0;
}
Well, so far, so good... But, something weird's happening when I change
C::C() and use an initialization list instead of an assignment in the
body...
C::C(int v) : value(v) {
cout<<"C() , "<<value<<endl;
}
GCC (version 3.3) is really not happy with that!:
proggy.cpp:41: error: class `C' does not have any field named `value'
Ok, _now_ I'm confused! I thought derived classes had access to all
non-private data of their base class?
I guess there is something absolutely trivial that I'm missing here...
Yeah, hard to believe I'm been using C++ for years, isn'it? (albeit on a
_very_ on and -mostly- off basis...)
Is there someone to help?
NT