473,385 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Using class initialization to initalize inherited attributes

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.

Feb 10 '06 #1
5 2366
si***************@gmail.com wrote:
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
--
Please remove capital As from my address when replying by mail
Feb 10 '06 #2
si***************@gmail.com wrote:
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
--
I'm not just a number. To many, I'm known as a string...
Feb 10 '06 #3
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.

Feb 14 '06 #4
si***************@gmail.com wrote:
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
--
Please remove capital As from my address when replying by mail
Feb 14 '06 #5
si***************@gmail.com wrote:
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
--
I'm not just a number. To many, I'm known as a string...
Feb 14 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

50
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong...
5
by: Carlos Ribeiro | last post by:
Hello all, I'm posting this to the list with the intention to form a group of people interested in this type of solution. I'm not going to spam the list with it, unless for occasional and...
12
by: Sacha Schär | last post by:
Suppose I have an array of Foo's: class Foo { public: int SomeMethode(void); int i; char c; char * p;
5
by: Garmt de Vries | last post by:
I have a table listing various translations of the titles of a set of books. Each row represents a book, each column represents a language. It looks like this: ...
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
2
by: feng | last post by:
Hi, I have a class, say class A, that has <Serializable()> attribute applied to it. Now if I create a class B that inherits from A, does B automaticly inherits this attribute also? or do I have...
53
by: Hexman | last post by:
Hello All, I'd like your comments on the code below. The sub does exactly what I want it to do but I don't feel that it is solid as all. It seems like I'm using some VB6 code, .Net2003 code,...
2
by: Tugrul HELVACI | last post by:
I'm using Delphi 2006 and I have a class defination like this: TPerson = class fPersonName : String; fPersonSurName : String; fPersonAge : Integer; published property PersonName : String...
8
by: bukzor | last post by:
I want to make a MixIn class that waits to initialize its super- classes until an attribute of the object is accessed. Not generally useful, but desirable in my case. I've written this, and it...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.