Skip to main content
gamedev.net
🔒 Locked

Calling constructors from constructors

Started by Endar Dec 14, 2005 at 4:17 AM 3 replies 800+ views
Original Post
Endar
Endar
So, calling an object's constructor from another of the object's constructor. Bad practice? Not for an object with dynamic memory, that would just be stupid. Here, I'll give an example:


CMatrix::CMatrix( float a, float b, float c, float d, float e, float f, float g, float h,
		float i, float j, float k, float l, float m, float n, float o, float p);

CMatrix::CMatrix(const util::CVector3d& v1, const util::CVector3d& v2, 
		 const util::CVector3d& v3, const util::CVector3d& v4 = util::CVector3d(0,0,0));


Two constructors. In the first one, it assigns each element of the array (on stack, not heap). In the second, it takes 4 vectors which each have 3 elements and those elements are assigned to the matrix elements. So, in the second one, should I specify "mat [0][0] = v1.x;", etc, for every element? Or should I just call the first constructor with the values?
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
Sneftel
Sneftel
"Calling a constructor" won't do what you think it does. But it'll look like it's doing what you think it should do. Hilarity will ensue.

Check it, dogg:

struct A{    A(int i) { m_i = i; }    A() { A(3); }    int m_i;};

Now, what does the default constructor do? If you said "sets its m_i member to 3", no cake for you. What A(3) does is, it constructs an unnamed temporary A using the integer constructor, and immediately destroys it. The original object has nothing done to it in the constructor.

You cannot write constructors for a given class in terms of each other. What you can do is (a) provide default arguments to constructors where appropriate, or (b) use private utility functions to actually construct the object. Of course, it's unwieldy to use utility functions in member initializer lists. The Named Constructor idiom can help too, but is also a little unwieldy if you're not used to it. There's C++ for you.
Endar
Endar
Wow. I've been using C++ for more than a year and a half. Not more than 5 minutes ago I wrote code that simply called the constructor of an object to create a temporary.

How in the hell did I think that would work?

Is it possible to delete this thread? I don't want everyone to read this and see how many kinds of an idiot I am. [smile]
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
Sneftel
Sneftel
You would be amazed what percentage of good, experienced C++ programmers don't know what that code really does. I would guess at least a third. After all, it looks so natural! And somewhere, Bjarne Stroustrup giggles.

EDIT: The best part of this error is when the programmer traces through it with the debugger, and sees that the integer constructor is indeed being called! And m_i is indeed being set! And then, a few instructions later, suddenly m_i appears to have mysteriously reset itself. The changing this pointer (easy to miss), and the destructor firing (may not even have one), are the only clues to what's really going on.
ghostbear77
ghostbear77
better to keep it... before reading about your (self admitted) stupidity i thought that this method wouldn't be a bad idea. i've been proven wrong.
r++ both of you for preventing that error on my side :D
< enter some witty programmer's one-liner here >

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.