Thread: Initializing inner-objects of base class from the driven-class constructor

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    241

    Initializing inner-objects of base class from the driven-class constructor

    my question as follows:

    let's say I have a Car object , and it contains inner Engine object.

    Code:
    struct Car{
    Engine mEngine;
    };
    in order to initialize the engine object NOT by the default constructor (if it has any) , we use initialization semantics:

    Code:
    Car::Car:
    mEngin(arg1,arg2,...)
    {
    other stuff here
    }
    now it gets tricky:
    let's say a Car objects has 10 inner objects, each object has about 5 variables in it . Car is a base class for , e.g. , Toyota class. you don't want the Car class to have a constructor with 50 arguments.
    can the inner objects of Car be initialized from the base class , e.g. Toyota?

    Code:
    class Toyota:
    Car(...),
    mEngine(...),
    mGear(..)
    {
    ...
    };
    the other options are:
    1) like said , create a Car constructor which gets 50 arguments, then initialize Car as whole from Toyota - the code becomes less readable and less intuitive
    2) Car constructor which get built-objects as arguments and initialize the inner objects with copy constructor . the code gets more readable but then you create many excess objects .
    I don't like either of these sulotions.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Dave11
    2) Car constructor which get built-objects as arguments and initialize the inner objects with copy constructor . the code gets more readable but then you create many excess objects .
    This is the way to go, except that instead of the copy constructor the move constructor for the member objects could be implemented and used to avoid unnecessary deep copying.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Firstly, the Engine member of your Car class is not an "inner object". The Car might be an aggregate of objects, one of which might be an engine. In C++, the Engine might be described as a member of the Car. It is not an "inner object".

    Second, a derived class does not need to directly initialise members of a base class - and, in practice, very rarely will. The derived class constructor will always invoke a base class constructor, either explicitly (i.e. via initialiser list) or implicitly (the compiler works out which constructor needs to be invoked). (A derived class may need to explicitly initialise virtual bases, but that's not what you are referring to - and is different from initialising members of a base class directly).

    There is nothing preventing a constructor, of any class, from accepting arguments that are of struct/class type (e.g. a struct that contains data to initialise several parts that will be components of a car) or a collection (for example, a vector containing various components). All the implementation of derived class constructor needs to do is selectively pass parameters it receives on to the base class.

    As laserlight said, the second way you describe is the way to go.

    Part of the skill (or the art form) of designing classes is avoiding undue complexity in how they are initialised (or, for that matter, how they are used). From your complaint, I suspect that is where you need to focus attention and learn. The only "excess objects" you need to worry about are those which you have put into your design "just in case" when they are not needed. If an object (or object type) is needed, then it is needed .... and, by definition, it is not excess.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Dec 2013
    Posts
    241
    By excess I mean temporary objects that will be deleted when the copying is over , thus wasting (temporarily) memory and increase running time.
    but then , computers are strong and fast today , and smart compilers will optimize all of the construction to death anyway..

    thanks all!

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Dave11 View Post
    By excess I mean temporary objects that will be deleted when the copying is over , thus wasting (temporarily) memory and increase running time.
    Even on old machines, such temporaries are rarely significant. In order to use a value, or a set of values, or an organised set of named values (aka a struct) to initialise something else, it is necessary to copy the values around. That is just as true in either of the ways you describe.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Dave11 View Post
    By excess I mean temporary objects that will be deleted when the copying is over , thus wasting (temporarily) memory and increase running time.
    That is what move semantics were made for.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 07-25-2008, 04:37 AM
  2. Replies: 9
    Last Post: 06-20-2008, 02:41 AM
  3. Initializing members of base class
    By anon in forum C++ Programming
    Replies: 8
    Last Post: 08-23-2007, 03:57 PM
  4. base class pointer to derived class objects
    By curlious in forum C++ Programming
    Replies: 4
    Last Post: 09-28-2003, 08:39 PM
  5. Calling constructor of the base class of a derived class..
    By CaptainPenguin in forum C++ Programming
    Replies: 5
    Last Post: 02-19-2003, 01:47 PM