Skip to main content
gamedev.net
🔒 Locked

calling constructor from other constructor

Started by soehrimnir Dec 4, 2001 at 6:29 AM 8 replies 1.3k views
Original Post
soehrimnir
soehrimnir
Hello everyone, I have the following problem. How do I call a constructor from within another constructor of the same class. To make it even clearer, take a look at the source:
  
class Test
{
public:
  Test()
  {
    // here I want to call the other constructor

    // Test(100);   this won''t work, because it creates a new Test object, leaving me with two test objects.

  }

  Test(int field)
  {
    this->field = field;
  }

private:
  int field;
};
  
Of course, for such a simple example I could just do field = 100, instead of calling the other constructor, but where I need it in my program, it is more convenient to do it with the constructor. Thanks
Mayrel
Mayrel
You don''t get to do this. C++ refuses to treat constructions like the functions they are.

The only way you''re going to get this working is to have the constructors call real functions that do their initialisation, so ''Test(int x)'' would call ''init(x)'', and then ''Test()'' would called ''init(100)''.



Just Plain Wrong
polly
polly
I really wouldnt do that.
Just have field = 100. Then you end up with only 1 object where the field value is 100, which is what you want, right?

Jon


Edited by - jonpolly99 on December 4, 2001 7:51:12 AM
soehrimnir
soehrimnir
Hey,

thanks for the answers. No Jon, in this example it would be the easiest solution, but in my program I have this situation.

  
class Test
{
Test()
{
f1 = f2 = f3 = f4 = f5 = f6 = f7 = f8 = f9 = 0;
}
Test(int f1)
{
this->f1 = f1;
f2 = f3 = f4 = f5 = f6 = f7 = f8 = f9 = 0;
}
Test(int f1, int f3)
{
this->f1 = f1;
this->f3 = f3
f2 = f4 = f5 = f6 = f7 = f8 = f9 = 0;
}

private:
int f1, f2, f3, f4, f5, f6, f7, f8, f9;
};


Now, my code doesn''t have 9 ints of course, it has lots of different types (Strings, images, RECTs, ...). So I have to initialize them all separately. Then I also have five different constructors and I hate to see the five initializations the same time (with some minor changes).

So, I guess I''ll have to use the idea of the Init() method Mayrel proposed.
Prosper/LOADED
Prosper/LOADED
What about this :

class Test
{
Test( int a=0, int b=0, ...);
};
Oluseyi
Oluseyi
quote:
Original post by Prosper/LOADED
class Test
{
Test( int a=0, int b=0, ...);
};

Default arguments is a good suggestion. The only thing is that the order is fixed, meaning you can''t specify override values for arguments 1 and 3, omitting 2. Other than that, though, this might be the best solution.
soehrimnir
soehrimnir
quote:
Original post by Anonymous Poster
not sure if this is smart but what about placement new? Something like new(this)();


That doesn''t look to good. I don''t even know what you are trying to tell us .

Anyway, that thing with using default arguments is not going to help me either, just because of the problem that Oluseyi mentioned.
That means, I''ll still be using the Init() method.
Shannon Barber
Shannon Barber
I was going to suggest placement new as well (which, btw Mayrel, is how you treat constructors like the functions that they are ) It's not any more flexible than default arguements, so if that doesn't help, placement new probably won't either.

The hidden Init method is what is suggested in Effective C++.

    
//Placement new for @#$#$ & giggles

#include <new>

class Test
{
public:
Test(int i) : m_i(i)
{}
Test()
{
//p is unneccassary becuase this still

// points to the same place

Test* p = new(this) Test(100);
}
private:
int m_i;
};


Magmai Kai Holmlor
- Not For Rent

Edited by - Magmai Kai Holmlor on December 4, 2001 12:16:45 PM
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
burp
burp
What about templates?

  
class null
{
};
//

template <class T1, class T2> class Test
{
public:
T1 t1;
T2 t2;

Test()
{}

Test( T1 a ): t1(a)
{}

Test( T1 a, T2 b ): t1(a), t2(b)
{}
};
//

void main(void)
{
Test<null,null> Test1();
Test<int,null> Test2(100);
Test<int,char*> Test3(100,"one hundred");
Test<char*,null> Test4("two hundred");

printf("%d\n",Test2.t1);
printf("%d, %s\n",Test3.t1,Test3.t2);
printf("%s\n",Test4.t1);
getchar();
}

Topic Locked

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

Sign in to reply to this topic.