Can one constructor call another?

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

  1. brekehan

    brekehan Guest

    //---------------------------------------------------------------------------------
    TextureCoordRect::TextureCoordRect(D3DXVECTOR2 topLeft, D3DXVECTOR2
    bottomRight)
    {
    if( topLeft.x < 0.0f || topLeft.x > 1.0f || topLeft.y < 0.0f ||
    topLeft.y > 1.0f ||
    bottomRight.x < 0.0f || bottomRight.x > 1.0f || bottomRight.y < 0.0f
    || bottomRight.y > 1.0f)

    throw Error("Attempting to create texture coordinate rect with
    invalid coordinates", "TextureCoordRect::TextureCoordRect(D3DXVECTOR2
    topLeft, D3DXVECTOR2 bottomRight)", "Font2D.cpp");

    m_topLeft = topLeft;
    m_bottomRight = bottomRight;
    }

    //---------------------------------------------------------------------------------
    TextureCoordRect::TextureCoordRect(float topLeft_u, float topLeft_v,
    float bottomRight_u, float bottomRight_v)
    {
    D3DXVECTOR2 topLeft(topLeft_u, topLeft_v);
    D3DXVECTOR2 bottomRight(bottomRight_u, bottomRight_v);

    // Someway to call the constructor that takes D3DXVECTOR arguments
    now?
    }
    brekehan, Jan 12, 2007
    #1
    1. Advertising

  2. brekehan wrote:
    > //---------------------------------------------------------------------------------
    > TextureCoordRect::TextureCoordRect(D3DXVECTOR2 topLeft, D3DXVECTOR2
    > bottomRight)
    > {
    > if( topLeft.x < 0.0f || topLeft.x > 1.0f || topLeft.y < 0.0f ||
    > topLeft.y > 1.0f ||
    > bottomRight.x < 0.0f || bottomRight.x > 1.0f || bottomRight.y < 0.0f
    > || bottomRight.y > 1.0f)
    >
    > throw Error("Attempting to create texture coordinate rect with
    > invalid coordinates", "TextureCoordRect::TextureCoordRect(D3DXVECTOR2
    > topLeft, D3DXVECTOR2 bottomRight)", "Font2D.cpp");
    >
    > m_topLeft = topLeft;
    > m_bottomRight = bottomRight;
    > }
    >
    > //---------------------------------------------------------------------------------
    > TextureCoordRect::TextureCoordRect(float topLeft_u, float topLeft_v,
    > float bottomRight_u, float bottomRight_v)
    > {
    > D3DXVECTOR2 topLeft(topLeft_u, topLeft_v);
    > D3DXVECTOR2 bottomRight(bottomRight_u, bottomRight_v);
    >
    > // Someway to call the constructor that takes D3DXVECTOR arguments
    > now?
    > }


    It's an FAQ:

    http://www.parashift.com/c -faq-lite/ctors.html#faq-10.3

    Best regards,

    Tom
    Thomas Tutone, Jan 12, 2007
    #2
    1. Advertising

  3. brekehan

    Simon G Best Guest

    brekehan wrote:
    >
    > //---------------------------------------------------------------------------------
    > TextureCoordRect::TextureCoordRect(float topLeft_u, float topLeft_v,
    > float bottomRight_u, float bottomRight_v)
    > {
    > D3DXVECTOR2 topLeft(topLeft_u, topLeft_v);
    > D3DXVECTOR2 bottomRight(bottomRight_u, bottomRight_v);
    >
    > // Someway to call the constructor that takes D3DXVECTOR arguments
    > now?
    > }


    I doubt it's a good idea to have one constructor invoke another on the
    same object. (I'm pretty sure it's a bad idea.)

    What you /could/ do is have a private member function to help the
    constructors, like this:-

    [Start C++ snippet.]

    class TextureCoordRect {
    private:
    void constructor_helper(D3DXVECTOR2 topLeft, D3DXVECTOR2 bottomRight);
    // ...
    };

    void TextureCoordRect::constructor_helper
    (D3DXVECTOR2 topLeft, D3DXVECTOR2 bottomRight) {

    // This is just taken from your first constructor.

    if(topLeft.x < 0.0f || topLeft.x > 1.0f ||
    topLeft.y < 0.0f || topLeft.y > 1.0f ||
    bottomRight.x < 0.0f || bottomRight.x > 1.0f ||
    bottomRight.y < 0.0f || bottomRight.y > 1.0f) {
    throw Error("Attempting to create texture coordinate rect with
    invalid coordinates",
    "TextureCoordRect::TextureCoordRect(D3DXVECTOR2
    topLeft, D3DXVECTOR2 bottomRight)",
    "Font2D.cpp");
    }
    m_topLeft = topLeft;
    m_bottomRight = bottomRight;
    }

    TextureCoordRect::TextureCoordRect
    (D3DXVECTOR2 topLeft, D3DXVECTOR2 bottomRight) {
    constructor_helper(topLeft, bottomRight);
    }

    TextureCoordRect::TextureCoordRect
    (float topLeft_u, float topLeft_v,
    float bottomRight_u, float bottomRight_v) {
    D3DXVECTOR2 topLeft(topLeft_u, topLeft_v);
    D3DXVECTOR2 bottomRight(bottomRight_u, bottomRight_v);
    constructor_helper(topLeft, bottomRight);
    }

    [End C++ snippet.]

    :)

    --
    Simon G Best
    What happens if I mention Leader Kibo in my .signature?
    Simon G Best, Jan 12, 2007
    #3
  4. brekehan

    Salt_Peter Guest

    The Subject Line's purpose is not meant to ask a question, ask you
    question in the body.
    Also, Its best to present a compileable summary of the issue.

    brekehan wrote:
    > //---------------------------------------------------------------------------------
    > TextureCoordRect::TextureCoordRect(D3DXVECTOR2 topLeft, D3DXVECTOR2
    > bottomRight)
    > {
    > if( topLeft.x < 0.0f || topLeft.x > 1.0f || topLeft.y < 0.0f ||
    > topLeft.y > 1.0f ||
    > bottomRight.x < 0.0f || bottomRight.x > 1.0f || bottomRight.y < 0.0f
    > || bottomRight.y > 1.0f)
    >
    > throw Error("Attempting to create texture coordinate rect with
    > invalid coordinates", "TextureCoordRect::TextureCoordRect(D3DXVECTOR2
    > topLeft, D3DXVECTOR2 bottomRight)", "Font2D.cpp");
    >
    > m_topLeft = topLeft;
    > m_bottomRight = bottomRight;
    > }


    Note the const references and init list:

    TextureCoordRect::TextureCoordRect( const D3DXVECTOR2& topLeft,
    const
    D3DXVECTOR2& bottomRight)
    : m_topLeft(
    topLeft ),

    m_bottomRight( bottomRight )
    {
    ...
    }

    >
    > //---------------------------------------------------------------------------------
    > TextureCoordRect::TextureCoordRect(float topLeft_u, float topLeft_v,
    > float bottomRight_u, float bottomRight_v)
    > {
    > D3DXVECTOR2 topLeft(topLeft_u, topLeft_v);
    > D3DXVECTOR2 bottomRight(bottomRight_u, bottomRight_v);
    >
    > // Someway to call the constructor that takes D3DXVECTOR arguments
    > now?
    > }


    TextureCoordRect::TextureCoordRect( float topLeft_u,
    float
    topLeft_v,
    float
    bottomRight_u,
    float
    bottomRight_v )
    {
    TextureCoordRect( D3DXVECTOR2(topLeft_u, topLeft_v),
    D3DXVECTOR2(bottomRight_u,
    bottomRight_v) );
    }

    // Although that should really be:

    TextureCoordRect::TextureCoordRect( float topLeft_u,
    float
    topLeft_v,
    float
    bottomRight_u,
    float
    bottomRight_v )
    : m_topLeft( topLeft_u, topLeft_v ),
    m_bottomRight( bottomRight_u,
    bottomRight_v )
    {
    }

    But you've made a fundamental mistake of performing a range check in
    TextureCoordRect when that check should have been in the D3DXVECTOR2
    constructor. That should have been evident since the if condition is
    duplicating code (same check for Left and Right).
    Salt_Peter, Jan 12, 2007
    #4
  5. Simon G Best wrote:
    >
    > I doubt it's a good idea to have one constructor invoke another on the
    > same object. (I'm pretty sure it's a bad idea.)


    It's more than a bad idea. It can't be done (at least not without some
    ugly hack, like described in the FAQ).

    Best regards,

    Tom
    Thomas Tutone, Jan 12, 2007
    #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. jeffc
    Replies:
    0
    Views:
    677
    jeffc
    Aug 26, 2003
  2. Matt Graham
    Replies:
    3
    Views:
    504
    Matt Graham
    Feb 7, 2004
  3. Andy
    Replies:
    9
    Views:
    499
    Pete Becker
    Sep 24, 2006
  4. Generic Usenet Account
    Replies:
    10
    Views:
    2,205
  5. Warren Tang
    Replies:
    9
    Views:
    662
    James Kanze
    Apr 15, 2008
Loading...

Share This Page