Bug? Parametrized constructor invokes default constructor

M

mario.rossi

Hi all,
I am trying to invoke the default constructor from another, parametrized,
constructor, but the default constructor doesn't get invoked at all, I saw.
Is this correct ISO C++ behaviour or my compiler has a ++bug (overflowing)?

struct Test {
int x;
int y;
//
Test() {
x=1;
y=2;
}
Test(int a) {
Test();
x+=a;
}
};

now, if I do:

Test Instance;

I will get 1 in x and 2 in y, as I expect.

But if I do:

Test Instance(3);

I will get garbage both in x and y, actually in x I will get garbage+3
(so to speak) and in y I will get garbage (the value that the stack
contained from previous use).

Ain't it possible to invoke the default constructor from another
constructor, or my compiler (VC++2005) has just another bug (TM)?

Thanks,
Mario
 
J

jjds101

Hi all,
I am trying to invoke the default constructor from another, parametrized,
constructor, but the default constructor doesn't get invoked at all, I saw.
Is this correct ISO C++ behaviour or my compiler has a ++bug (overflowing)?

struct Test {
int x;
int y;
//
Test() {
x=1;
y=2;
}
Test(int a) {
Test();
x+=a;
}
};

now, if I do:

Test Instance;

I will get 1 in x and 2 in y, as I expect.

But if I do:

Test Instance(3);

I will get garbage both in x and y, actually in x I will get garbage+3
(so to speak) and in y I will get garbage (the value that the stack
contained from previous use).

Ain't it possible to invoke the default constructor from another
constructor, or my compiler (VC++2005) has just another bug (TM)?

http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.3
 
M

mlimber

LR said:
Excuse me but wasn't there a thread about this just a week or two ago.
I asked then, but got no reply, if it would be possible to do this:

(I didn't try to compile this)
struct Test {
int x, y;
void swap(Test &t) {
std::swap(t.x,x);
std::swap(t.y,y);
}
Test() : x(1), y(2) {}
Test(int a) {
Test temp;
swap(temp);
x += a;
}
};

Is this valid?

Yes, you create an extra object that is default initialized and then
swap its member values for the current object's. There are better ways
to accomplish the same thing than writing obscure code. See, e.g., at
the end of this FAQ:

http://parashift.com/c++-faq-lite/ctors.html#faq-10.3

Cheers! --M
 
S

Salt_Peter

mlimber said:
Yes, you create an extra object that is default initialized and then
swap its member values for the current object's. There are better ways
to accomplish the same thing than writing obscure code. See, e.g., at
the end of this FAQ:

http://parashift.com/c++-faq-lite/ctors.html#faq-10.3

Cheers! --M

In this case, even that is overkill:

struct Test
{
int x;
int y;
public:
Test() : x(1), y(2) { }
Test::Test(int a) : x(a+1), y(2) { }
};

would do.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,044
Latest member
RonaldNen

Latest Threads

Top