copy constructor: initialize the base

Discussion in 'C++' started by brekehan, Jan 19, 2007.

  1. brekehan

    brekehan Guest

    My C++ has gotten so rusty!!!
    If my constructor looks like this:

    Image2D::Image2D(LPDIRECT3DDEVICE9 device)
    :
    SceneObject2D(device),
    <snip>


    How do I do my copy constructor...I am not sure how to initialize the
    base outside of the initializer list..

    Image2D::Image2D(const Image2D & rhs)
    :
    m_path(rhs.m_path),
    m_rows(rhs.m_rows),
    m_cols(rhs.m_cols),
    m_transparent(rhs.m_transparent),
    m_active(rhs.m_active),
    m_numFrames(rhs.m_numFrames),
    m_currentFrame(rhs.m_currentFrame),
    m_position(rhs.m_position),
    m_depth(rhs.m_depth),
    m_width(rhs.m_width),
    m_height(rhs.m_height)
    {

    if(rhs.m_device)
    {
    rhs.m_device->AddRef();

    // Is this the way to construct my base or is it
    // contructing something that never gets used???
    SceneObject2D(rhs.m_device);
    }
    }
     
    brekehan, Jan 19, 2007
    #1
    1. Advertising

  2. brekehan

    Rolf Magnus Guest

    brekehan wrote:

    > My C++ has gotten so rusty!!!
    > If my constructor looks like this:
    >
    > Image2D::Image2D(LPDIRECT3DDEVICE9 device)
    > :
    > SceneObject2D(device),
    > <snip>
    >
    >
    > How do I do my copy constructor...I am not sure how to initialize the
    > base outside of the initializer list..


    You don't. You initialize it in the initializer list.

    > Image2D::Image2D(const Image2D & rhs)
    > :
    > m_path(rhs.m_path),
    > m_rows(rhs.m_rows),
    > m_cols(rhs.m_cols),
    > m_transparent(rhs.m_transparent),
    > m_active(rhs.m_active),
    > m_numFrames(rhs.m_numFrames),
    > m_currentFrame(rhs.m_currentFrame),
    > m_position(rhs.m_position),
    > m_depth(rhs.m_depth),
    > m_width(rhs.m_width),
    > m_height(rhs.m_height)
    > {
    >
    > if(rhs.m_device)
    > {
    > rhs.m_device->AddRef();
    >
    > // Is this the way to construct my base or is it
    > // contructing something that never gets used???
    > SceneObject2D(rhs.m_device);


    It creates a temporary SceneObject2D that is destroyed immediately
    afterwards. As soon as you enter the consturctor body, base class part and
    member variable initialization is finised.

    > }
    > }
     
    Rolf Magnus, Jan 19, 2007
    #2
    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:
    610
    Larry I Smith
    May 18, 2005
  2. ali
    Replies:
    4
    Views:
    586
    David Harmon
    Mar 5, 2007
  3. Rahul
    Replies:
    16
    Views:
    1,221
    terminator
    Nov 7, 2007
  4. Replies:
    2
    Views:
    720
  5. cinsk
    Replies:
    35
    Views:
    2,651
    James Kanze
    Oct 11, 2010
Loading...

Share This Page