-
Object as Member of another class
Hello all expert C++ programmer, i have a noob question about object as member of another class.
For instance, there are two class which is class date and class employee. Class employee contains date object as a data member.
As i know from drybelk, he say that we cannot do this. We must use pointer or reference. This thread is located in this website but i cannot find. Therefore i start a new thread.
What confused me is i have read a book where do the opposite way which is class employee contains date object.
As far as i know, we must used member initialization list.
How come the book saying can include object as another class but drybelk say cannot ?
Sorry for english.
I hope you all can clear my doubts.
Thanks for your help.
A billion thanks for your help.
I very appreciated your help.
-
I don't think that's what Drybelk said. And besides, this isn't correct. You can certainly have an object as a member of another object:
class Date {
//..
};
class Employee
{
Date d;
...
};
Danny Kalev
-
As i know, this is calling has a relationship. Thanks for clearing my doubt.
A billion thanks to you. I very happy because i can elarn from you.
-
I think I remember what we were talking about:
You cannot have an object of the same class inside the class:
Code:
class Wrong
{
public:
Wrong w; // that is not possible!!!
};
but of course there is no problem with
Code:
class A
{};
class B
{
public:
A a;
};
This composition of objects is probably the most important feature in OO and in particular C++.
Sorry if my response to the thread you mentioned was confusing... probably got to do with my accent ;-)
Cheers,
D
DKyb-------------------------------
Life is a short warm moment -
Death is the long cold rest.
Pink Floyd
-------------------------------
-
This is not your fault. I have misunderstanding your sentence.
class A
{
A& rhs;
A* rhs;
}; We must use pointer or reference in case of we want a object of same class.
I very appreciated your help. You are a kind man, I hope GOD will blessed you.
-
I'm afraid a reference won't work, either. Pretty much the same reason. As you know a reference variable has to be initialized with an existing object.
So your constructor would go into an infinite loop causing some sort of stack overflow...
Haven't tried it, but would surprise me if a ref would work. Probably Danny can shed some light on it from a C++ - standard perspective?
-
You're right. A reference to A used as a member in class A is illegal. The problem is not recursion, but a chicken and the egg issue: conceptually, reference data members must be initialized before the object's constructor has run. In other words, they must be initialized in a member initialization list. However, the initializer of rhs must be a valid A object, i.e., *this, and the expression *this is considered a valid object only once the constructor has finished executing.
Because of this, the code as is will not compile. Making rhs a reference to const might make it compile but the code will still have undefined behavior because the program is forced to initialize rhs with an object that hasn't been fully constructed:
class A
{
const A& rhs;
public:
A(): rhs (*this /*not valid yet*/) {} //undefined behavior
};
Last edited by Danny; 08-07-2007 at 11:01 AM.
Danny Kalev
-
Similar Threads
-
Replies: 26
Last Post: 12-01-2012, 04:12 AM
-
Replies: 5
Last Post: 01-15-2006, 07:10 PM
-
Replies: 1
Last Post: 05-23-2002, 07:50 AM
-
By don in forum VB Classic
Replies: 1
Last Post: 10-22-2000, 06:54 AM
-
By Tom Shreve in forum Enterprise
Replies: 0
Last Post: 04-07-2000, 08:15 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks