0
votes

I have a question about calling a constructor in an initialization list of another constructor. For example when looking at code like this:

Library::Child::Child
380 (
381  const word& controlName,
382  const argList& args,                         
383  const word& systemName,              
384  const word& constantName            
385 )
386 :       
387  Parent                                        
388  (                                                         
389  args.rootPath(),                                
390  args.caseName(),                              
391  systemName,                                     
392  constantName                                    
394  )
395 {}  

with the constructor of class Parent called in line 387:

   30 Library::Parent::Parent
   31 (
   32     const fileName& rootPath, 
   33     const fileName& caseName,
   34     const word& systemName,
   35     const word& constantName
   36 )
   37 :
   38     processorCase_(caseName.find("processor") != string::npos), //private member
   39     rootPath_(rootPath),  //private member
   40     case_(caseName),     //private member
   41     system_(systemName),   //private member
   42     constant_(constantName)  //private member
   43 {}        

The primary object constructed is of type/class Library::Child::Child, but when constructing that object also the constructor Library::Parent::Parent of parentclass Parent is called and private members of class Parent are defined. Since class Child does not inherit private member of class Parent and furthermore when calling the constructor of class Parent no object/variable is declared (see line 387, first code snippet) I don't know what calling the constructor of Parent is good for? Furthermore in what connection are the private members of class Parent to the class Child object and where are they stored since no class Parent object is declared? I tried to find information about it in the net, but could not find anything answering my questions.

greetings streight

1
Library::Child::Child is a derived class from the base class Library::Parent::Parent ?? (The words Base and Derived would make this question considerably easier to follow if that is, in fact, the case). - WhozCraig
@WhozCraig : Yes, class Child is derived from class Parent. I think this is also very easy to understand and in this case accurate since Child inherits directly from Parent. Note: Library::Child::Child is the constructor and not the class, but I am sure you know this. - Streight

1 Answers

1
votes

You seem to missunderstand some basic concepts in c++. A derived class inherits everything from its parent class. It might not have access to it. It is not like "oh its private so it does not exists".

I think rest of your problems come from this missconception. And maybe from what declaration and definition mean. You can imagine derivation in c++ like extending base class. In the memory the Derived stuff is kinda glued to Base.

The Derived stuff would have no sense if no Base were present. It is an extension to Base. Thus, Base class object has always be instantiated. Even if the derived class has no access to it. That is why the constructor of Base is called during the construction of Derived. It is always called. Even if not explicitly, then implicitly default constructor will be called.

The Child constructor calls Parent constructor, and does not access any private members. The Parent::Parent() does. It does not define them it initialized them, they are defined in Parent class definition.

Also objects are always declared. You cannot derive from an undeclared object. Nor you can do anything with an undeclared object. So your Parent must be declared, otherwise compiler would throw an error.