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:
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!