Object as Member of another class


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: Object as Member of another class

Hybrid View

  1. #1
    Join Date
    May 2007
    Posts
    843

    Thumbs up 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.

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    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

  3. #3
    Join Date
    May 2007
    Posts
    843
    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.

  4. #4
    Join Date
    Jan 2005
    Location
    UK
    Posts
    604
    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
    -------------------------------

  5. #5
    Join Date
    May 2007
    Posts
    843
    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.

  6. #6
    Join Date
    Jan 2005
    Location
    UK
    Posts
    604
    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?

  7. #7
    Join Date
    Nov 2003
    Posts
    4,118
    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

  8. #8
    Join Date
    May 2007
    Posts
    843
    Thanks your very much.

Similar Threads

  1. Replies: 26
    Last Post: 12-01-2012, 04:12 AM
  2. JDOM Classpath Help Required
    By kpandya in forum Java
    Replies: 5
    Last Post: 01-15-2006, 07:10 PM
  3. Object Array ? Do you Worth it
    By ashish in forum Java
    Replies: 1
    Last Post: 05-23-2002, 07:50 AM
  4. Replies: 1
    Last Post: 10-22-2000, 06:54 AM
  5. Re: COM object Password Security
    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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center