Using class initialization to initalize inherited attributes

S

silverburgh.meryl

How come I an not use class initialization to initalize inherited
attributes?

i have this code:
B::B(A& a) {
x = a.x;
y = a.y;
}

class A {
public:
int x;
int y;
};

class B: public A{
public:
B(A& a);
};

and I get this compile error:

B.cpp:34: error: class 'B' does not have any field named 'x'
Bc.pp:35: error: class 'B' does not have any field named 'y'

can someone please tell me why?
Thank you.
 
V

Victor Bazarov

How come I an not use class initialization to initalize inherited
attributes?

i have this code:
B::B(A& a) {
x = a.x;
y = a.y;
}

class A {
public:
int x;
int y;
};

class B: public A{
public:
B(A& a);
};

and I get this compile error:

B.cpp:34: error: class 'B' does not have any field named 'x'
Bc.pp:35: error: class 'B' does not have any field named 'y'

can someone please tell me why?

The code in the presented form will not even compile. Please post
the _actual_ code with which you have your problem.

V
 
B

Ben Pope

How come I an not use class initialization to initalize inherited
attributes?

i have this code:
B::B(A& a) {
x = a.x;
y = a.y;
}

class A {
public:
int x;
int y;
};

class B: public A{
public:
B(A& a);
};

and I get this compile error:

B.cpp:34: error: class 'B' does not have any field named 'x'
Bc.pp:35: error: class 'B' does not have any field named 'y'

can someone please tell me why?

Probably the order of the code, the following works fine:

class A {
public:
int x;
int y;
};

class B: public A{
public:
B(A& a);
};

B::B(A& a) {
x = a.x;
y = a.y;
}

int main() {
}


It's probably preferable to make x and y private and provide a
constructor for A, with an initialisation list, and then provide an
initialisation list for B (I've also provided another constructor for B):

class A {
public:
A(int x, int y) : x_(x), y_(y) {};
private:
int x_;
int y_;
};

class B: public A {
public:
B(int x, int y) : A(x, y) {}
B(A& a) : A(a) {};
};

int main() {
}


I hope that gives you an idea.

That way, you have kept encapsulation. (Only A can play with it's
privates, B cannot).

Ben Pope
 
S

silverburgh.meryl

Thanks. For your anwser below, it works:

B::B(A& a) {
x = a.x;
y = a.y;

}

but if I change it to initialization, like this, it won't compile.
B::B(A& a) :
x (a.x)
y (a.y) {

}

Any reason why this won't compile?

Thank you.
 
V

Victor Bazarov

Thanks. For your anwser below, it works:

B::B(A& a) {
x = a.x;
y = a.y;

}

but if I change it to initialization, like this, it won't compile.
B::B(A& a) :
x (a.x)
y (a.y) {

}

Any reason why this won't compile?

Because that syntax is reserved for base classes and members. Like
in good ol' medeival times: the member of my base is not my member.

V
 
B

Ben Pope

Thanks. For your anwser below, it works:

B::B(A& a) {
x = a.x;
y = a.y;

}

but if I change it to initialization, like this, it won't compile.
B::B(A& a) :
x (a.x)
y (a.y) {

}

Any reason why this won't compile?

Victor gave you the reason. I've already suggested why you don't want
to do that anyway, and provided a workaround.

The initialisation list is for members only, not everything that is
accessible from X.

Take a closer look at the second half of my previous response. If you
don't understand it, ask.

Ben Pope
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top