object and constructor

This is a discussion on object and constructor within the C++ Programming forums, part of the General Programming Boards category; Hello How can I solve the next problem: Code: class A { public: A() : m_b(m_c) { } B m_b; ...

  1. #1
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630

    object and constructor

    Hello

    How can I solve the next problem:

    Code:
    class A {
    public:
    A() : m_b(m_c) { }
    B m_b;
    C m_c;
    };
    
    class B {
    public:
    B(C &ref) : m_c(ref) { }
    C &m_c;
    };
    The problem is I want to pass a reference of object (class C) which wasnt 'built' yet to class B.
    What can I do about this?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    Place the declaration of m_b after m_c inside class A. Then m_c will be constructed first, and can be passed to m_b. Another option is to just use a (smart) pointer that can be assigned after construction.

    This design can be confusing and cause maintenance headaches, though, so be sure you need to do that.

  3. #3
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Program still crashes. Could it be because I pass *this along with the C's reference?

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by l2u View Post
    Program still crashes. Could it be because I pass *this along with the C's reference?
    You'll have to post the real code.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    There's nothing wrong with passing and storing a reference to an uninitialized object. You just can't use it until it has been initialized.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. when is the constructor of a global object called??
    By mynickmynick in forum C++ Programming
    Replies: 6
    Last Post: 08-28-2008, 04:57 AM
  2. Replies: 3
    Last Post: 08-13-2008, 01:34 PM
  3. DLL Global Object Constructor String Error
    By n00b3 in forum Windows Programming
    Replies: 1
    Last Post: 06-29-2008, 07:42 PM
  4. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 08:38 PM
  5. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 04:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21