Construstors: Called When and How in Other Objects?

This is a discussion on Construstors: Called When and How in Other Objects? within the C++ Programming forums, part of the General Programming Boards category; I'm having trouble understanding when constructors are called from within another object. I've been told it's best to declare (or ...

  1. #1
    Registered User HalNineThousand's Avatar
    Join Date
    Mar 2008
    Posts
    43

    Construstors: Called When and How in Other Objects?

    I'm having trouble understanding when constructors are called from within another object. I've been told it's best to declare (or is it define) an object in the header file and then include that header file in the source with that object. In other words, if I have ObjectA, then I should have ObjectA.h and ObjectA.cpp and in ObjectA.h I might have this:
    Code:
    //various #import and other statements
    ...
    class ObjectA {
    
    	ObjectB objb;
    	public:
    		doImportantStuff()
    
    };
    Then I'd have the actual functions in the source file. I have that down and I'm used to it. the problem I'm having is when it comes to understanding when constructors are called. For example, in the code above, since objb has a default constructor, when is that constructor called? I would think that I'd have to do this in the source file:
    Code:
    	objb = ObjectB();
    So far, so good, most of the time, because I have cases where that seems to work okay, but what if I don't use a default constructor for ObjectB and it requires a parameter, like, say, ObjectC? Then I have this in the ObjectA.h:
    Code:
    //various #import and other statements
    ...
    class ObjectA {
    
    	ObjectC objc;
    	ObjectB objb;
    	public:
    		doImportantStuff()
    
    };
    And let's say the constructor of ObjectB requires as a single parameter Object C.

    When I put code like this in my header for an object with a non-default constructor, I get errors. I thought, in this case, I was just declaring the variables that would be in obja and not yet setting values or constructing objects. I can't specify, here, for objb to use objc because neither one is completely defined by the rest of the code yet.

    I know how to make the non-default constructor, that's not too different from Java, but how do I declare objb in ObjectA.h if it has a non-default constructor? No matter what I do, it seems like the compiler expects that object to be constructed at that first reference.

    I'm sure I must be misunderstanding the situation or just not using the right syntax.

    What am I doing wrong and what do I need to fix it?

    Thanks!

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by HalNineThousand View Post
    I would think that I'd have to do this in the source file:
    Code:
    	objb = ObjectB();
    No, if you declare the member variable in your class definition, the default constructor will automatically get called when your class's constructor is called. The code you posted would create a temporary ObjectB object and assign it to an already default constructed ObjectB member variable.

    Quote Originally Posted by HalNineThousand View Post
    So far, so good, most of the time, because I have cases where that seems to work okay, but what if I don't use a default constructor for ObjectB and it requires a parameter, like, say, ObjectC?
    Then you can pass those parameters in your constructor initialization list:

    Code:
    ObjectA::ObjectA( const std::string&  name, int  age )
    :   objb( name ), objc( age )  // Initialization list
    {
       // ...
    }

  3. #3
    Registered User HalNineThousand's Avatar
    Join Date
    Mar 2008
    Posts
    43
    So if I include the parameters needed, it is still creating a temporary version of objb until I create the one I actually intend to use?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    When a constructor is called, it calls the constructor for all base classes (if any) and then for all member objects in the order the member objects are declared in the class. This constructs those objects. If you place the objects into the initializer list (shown in blue in cpjust's example) then the constructor will be called based on what you have there (although the order will still be the order of declaration inside the class). If you don't place them in the initializer list, then the default constructor will be called. Either way they will be constructed before the body ofthe constructor function starts.

    There is no temporary version in these cases unless you do something wrong.

    Can you post an actual example of something you've tried that failed?

  5. #5
    Registered User HalNineThousand's Avatar
    Join Date
    Mar 2008
    Posts
    43
    I wish I still had the examples, but in the interest of just getting things working, I used default constructors and put in some getters and setters to pass on the objects once it was constructed. While it works, I want to know how to make the non-default constructors work. I don't want to spend my C++ programming time dodging an issue and producing bad code because I'm ignorant!

    I'll try it again, using what I've learned here, and see how it works. That should be either later tonight or tomorrow when I get a chance to try it.

    Thanks, both, for the help. I feel like I know what's going on now and I'll see what I can do to make it work "right."

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    >> I want to know how to make the non-default constructors work.
    The initializer list is what you want, and it's good you're planning to figure it out since it is good practice.

Popular pages Recent additions subscribe to a feed

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