Passing reference to derived class object to base class constructor

Discussion in 'C++' started by Jeff, Mar 7, 2006.

  1. Jeff

    Jeff Guest

    The derived class below passes a reference to an object in its own
    class to its base calss constructor. The code compiles and will run
    successfully as long as the base class constructor does not attempt to
    access the object -- since m_object is not actually created and
    initizialized until after the base constructor has been called.

    Any thoughts on the practice below?
    class Base
    {
    public:
    Base(Object& ref) {};
    ~Base() {};
    };

    class Derived : public Base
    {
    public:
    Derived(int param) : Base(m_object), m_object(param) {};

    protected:
    Object m_object;
    };
    Jeff, Mar 7, 2006
    #1
    1. Advertising

  2. Jeff wrote:
    > The derived class below passes a reference to an object in its own
    > class to its base calss constructor. The code compiles and will run
    > successfully as long as the base class constructor does not attempt to
    > access the object -- since m_object is not actually created and
    > initizialized until after the base constructor has been called.
    >
    > Any thoughts on the practice below?


    Bad practice. Whoever comes later to do something with 'Base' is not
    aware that 'ref' cannot be used during construction. There are some
    situations (like if 'Base' as a member of type 'Object&' that cannot
    be set later, and has to be initialised) in which you have to pass the
    reference to the c-tor, but those cases need to be _extensively_
    documented in the code.

    On the second thought, it's possible that by redesigning one could work
    around this... Need to be dealt with on the case-by-case basis.

    > class Base
    > {
    > public:
    > Base(Object& ref) {};
    > ~Base() {};


    The semicolons after function bodies are unnecessary. And they really
    make the code look unclean.

    > };
    >
    > class Derived : public Base
    > {
    > public:
    > Derived(int param) : Base(m_object), m_object(param) {};
    >
    > protected:
    > Object m_object;
    > };
    >


    V
    --
    Please remove capital As from my address when replying by mail
    Victor Bazarov, Mar 7, 2006
    #2
    1. Advertising

  3. Jeff

    Fei Liu Guest

    Jeff wrote:
    > The derived class below passes a reference to an object in its own
    > class to its base calss constructor. The code compiles and will run
    > successfully as long as the base class constructor does not attempt to
    > access the object -- since m_object is not actually created and
    > initizialized until after the base constructor has been called.
    >
    > Any thoughts on the practice below?
    > class Base
    > {
    > public:
    > Base(Object& ref) {};
    > ~Base() {};
    > };
    >
    > class Derived : public Base
    > {
    > public:
    > Derived(int param) : Base(m_object), m_object(param) {};
    >
    > protected:
    > Object m_object;
    > };


    The standard requires base class initialized before derived class
    member object initialization. Therefore your code will crash if base
    constructor access m_object because m_object is not initialized yet.
    Fei Liu, Mar 7, 2006
    #3
  4. Fei Liu wrote:
    > Jeff wrote:
    >
    >>The derived class below passes a reference to an object in its own
    >>class to its base calss constructor. The code compiles and will run
    >>successfully as long as the base class constructor does not attempt to

    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    >>access the object -- since m_object is not actually created and

    ^^^^^^^^^^^^^^^^^
    >>initizialized until after the base constructor has been called.
    >>
    >>Any thoughts on the practice below?

    [...]
    > The standard requires base class initialized before derived class
    > member object initialization. Therefore your code will crash if base
    > constructor access m_object because m_object is not initialized yet.


    So, if A precedes B, then B must be following A. Did I get it right?
    Victor Bazarov, Mar 7, 2006
    #4
  5. Jeff

    Peter_Julian Guest

    "Jeff" <> wrote in message
    news:...
    | The derived class below passes a reference to an object in its own
    | class to its base calss constructor. The code compiles and will run
    | successfully as long as the base class constructor does not attempt to
    | access the object -- since m_object is not actually created and
    | initizialized until after the base constructor has been called.
    |
    | Any thoughts on the practice below?
    | class Base
    | {
    | public:
    | Base(Object& ref) {};
    | ~Base() {};
    | };
    |
    | class Derived : public Base
    | {
    | public:
    | Derived(int param) : Base(m_object), m_object(param) {};
    |
    | protected:
    | Object m_object;
    | };
    |

    If the base class needs a reference to a derived class's component, then
    thats a clear sign that that component should be in the base class instead.

    class Object { };

    class Base
    {
    Object m_object;
    public:
    Base(Object o) : m_object(o) { }
    virtual ~Base() = 0 { }
    virtual Object getObject() const = 0 { return m_object; }
    };

    class Derived : public Base
    {
    public:
    Derived(Object o) : Base(o) { }
    ~Derived() { }
    /* required pure-virtual interface */
    Object getObject() const { return Base::getObject(); }
    };

    int main()
    {
    Object obj;
    // Base b(obj); cannot instantiate abstract class
    Derived d(obj);

    return 0;
    }
    ____
    hmm, good candidate(s) for a template.

    template< class T >
    class Base
    {
    T m_t;
    public:
    Base(T t) : m_t(t) { }
    virtual ~Base() = 0 { }
    virtual T getT() const = 0 { return m_t; }
    };

    template< class T >
    class Derived : public Base< T >
    {
    public:
    Derived(T t) : Base(t) { }
    ~Derived() { }
    /* required pure-virtual interface */
    T getT() const { return Base::getT(); }
    };

    class Object { };
    class Object2 { };

    int main()
    {
    Object obj;
    Derived< Object > d(obj);

    Object2 obj2;
    Derived< Object2 > d2(obj2);

    return 0;
    }
    Peter_Julian, Mar 8, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mrstephengross
    Replies:
    5
    Views:
    579
    Larry I Smith
    May 18, 2005
  2. ali
    Replies:
    4
    Views:
    553
    David Harmon
    Mar 5, 2007
  3. Replies:
    1
    Views:
    378
    myork
    May 23, 2007
  4. Replies:
    1
    Views:
    369
    Victor Bazarov
    May 23, 2007
  5. Rahul
    Replies:
    16
    Views:
    1,171
    terminator
    Nov 7, 2007
Loading...

Share This Page