Sign in to follow this  

C++ constructors trick

This topic is 4843 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I don't know why in C++ it's not possible a thing like that:
class foo
{
   foo() { }
   foo(int bar) : foo() { }   //compiler error
}

I know that when the compiler parse the initialization list the foo object don't exists (yet) but for me it's a limitation, in fact it's the same class and the compiler know how to handle it no? Is there something wrong in my words? I have to write:
foo(int bar)
{
   foo();
   //other code
}

When you have a lot of parameters to initialize and a lot of overloaded constructors have the possibility of indicate a common costructor in the init list increase code readability (it's my opinion)

Share this post


Link to post
Share on other sites
The last code snippet doesn't do what you think it does. It constructs an unnamed foo object on the stack which is destroyed when the closing brace of the constructor is reached.

What you need is a constructor helper function (probably best to make it private) which initialises your type and call it from the body of both constructors. But you can't do that from the initializer list.

Share this post


Link to post
Share on other sites
C++ doesn't have invocable ctor's like Java does for initialization. C++ always invokes a base class ctor; you can specify a non-default base ctor in the initialization, but if you don't the default ctor (of the base class) is invoked.

You can move the common elements into a base class, and then they'll all be initialized the same way.

struct common_data
{
common_data() : x(0), y(1), z(2) {}
protected:
int x,y,z;
};

class my_class : common_data
{
};



But this should probably be a more explcit design:

struct point3d
{
point3d() : x(0), y(0), z(0) {}
int x,y,z;
};

class my_class
{
point3d location;
};



Generally speaking, if you have a lot of elements to initialize, you're design is lacking. Use the standard containes, use smart-pointers and much of your initialization goes away. If it's a data container, such as WaveFormat, then it should have methods to set compatible defaults, and you can call one in the ctor (e.g. cd_quality(), radio_quality(), fm_quality() ).

In C++ the objective is to have no initialization list - not because data isn't initialized, but because it's automatically generated by the compiler. Also recall that, again in general, C++ constructors should not perform complicated task; thier job is to place the object into a /known/ state not a "good" one (e.g. set the file handle to 0, not necessarily open a file).

Share this post


Link to post
Share on other sites
You wanna constructor trick:


#include <new>

struct foo {

int bar, bar2;

foo(): bar(0), bar2(0) { /* stuff */ }

foo(int i, int j) {

::new(this) foo();
bar = i, bar2 = j;
}

};


although this example is redundant because you could combine the default & non-default constructor by giving default values it shows you how you can reuse constructors by using placement new operator, you need to include the header new, i don't recommend doing this thou.

Share this post


Link to post
Share on other sites
Quote:
Original post by Magmai Kai Holmlor
That's "dangerous" if the sub-class allocated memory or acquires other resources.


yep, thats what i've already said i don't recommend doing it, there are better ways.

Share this post


Link to post
Share on other sites
Quote:
Original post by Paradigm Shifter
The last code snippet doesn't do what you think it does.


yeah you're right a little mistake from me. I right thing I'am saying is:

class foo
{
public:
foo();
foo(int bar) { init(bar); }
foo(int bar, int rep) : m_rep(rep) { init(bar); }
foo(int bar, int tor) : m_rep(rep), m_tor(tor) { init(bar); }
private:
//very bad
init(int bar) { m_bar = bar; }
int m_bar, m_rep, m_tor;
}

it's the only approch to take without ereditate from a base class... but it's very bad design.

Share this post


Link to post
Share on other sites

This topic is 4843 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this