Can one constructor call another?

B

brekehan

//---------------------------------------------------------------------------------
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?
}
 
T

Thomas Tutone

brekehan said:
//---------------------------------------------------------------------------------
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
 
S

Simon G Best

brekehan said:
//---------------------------------------------------------------------------------
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.]

:)
 
S

Salt_Peter

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.
//---------------------------------------------------------------------------------
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).
 
T

Thomas Tutone

Simon said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top