Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Can one constructor call another? (http://www.velocityreviews.com/forums/t459795-can-one-constructor-call-another.html)

brekehan 01-12-2007 08:06 PM

Can one constructor call another?
 
//---------------------------------------------------------------------------------
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(D3DXVECTOR 2
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?
}


Thomas Tutone 01-12-2007 08:27 PM

Re: Can one constructor call another?
 
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(D3DXVECTOR 2
> 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-lit....html#faq-10.3

Best regards,

Tom


Simon G Best 01-12-2007 08:51 PM

Re: Can one constructor call another?
 
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(D3DXVECTOR 2
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?

Salt_Peter 01-12-2007 09:00 PM

Re: Can one constructor call another?
 
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(D3DXVECTOR 2
> 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).


Thomas Tutone 01-12-2007 09:02 PM

Re: Can one constructor call another?
 

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



All times are GMT. The time now is 07:44 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.