473,473 Members | 1,947 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

Jan 12 '07 #1
4 3371
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

Jan 12 '07 #2
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?
Jan 12 '07 #3
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).

Jan 12 '07 #4

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

Jan 12 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: S³awek | last post by:
Can one constructor of an object call another constructor of the same class? class foo { foo(float f, int i) // a "full" constructor { ... } foo(int i) // a "simplified" constructor {
23
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
24
by: slurper | last post by:
i have the following class sequence { public: sequence (const sequence& mysequence, const int newjob) { job_sequence(mysequence.job_sequence) job_sequence.push_back(newjob); ... }
6
by: Fred Zwarts | last post by:
Hello, I am trying to debug some complex debug code. In order to track the use of dynamically allocated memory, I replaced the standard global new and delete operators. (Not for changing the...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
5
by: Wilfried Mestdagh | last post by:
Hi, I want to overload a constructor of a class. But I want to call the other one from the second if called. I explain with code because of my english: public class XMLConfig { public...
2
by: Sathyaish | last post by:
How does a constructor of one class call another of the same class? When would this mechanism make sense or be required? PS: Two instances I saw are: (a) While using the Singleton pattern...
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
13
by: sam_cit | last post by:
Hi Everyone, I have the following unit to explain the problem that i have, class sample { public : sample() { printf("in sample...\n"); }
9
by: Morten Lemvigh | last post by:
Is it possible to pass a pointer to a constructor or a class definition as argument to a function? Maybe in a way similar to passing function pointers...? The function should construct a number...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.