473,503 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Bug? Parametrized constructor invokes default constructor


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

Oct 12 '06 #1
4 1728

ma*********@REMOVETHISTOMAILMENOT.com wrote:
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-lit....html#faq-10.3

Oct 12 '06 #2
LR
jj*****@yahoo.com wrote:
ma*********@REMOVETHISTOMAILMENOT.com wrote:
>>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-lit....html#faq-10.3
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?

LR
Oct 12 '06 #3
LR wrote:
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

Oct 12 '06 #4

mlimber wrote:
LR wrote:
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
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.

Oct 12 '06 #5

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

Similar topics

5
9439
by: Michael McKnerney | last post by:
Hi, It seems I can influence how a base class is initialized beyond the 'normal' manner and I was wondering if someone can tell me why this. Here's my example. class A { public: A(int a=1,...
15
21173
by: A | last post by:
Hi, A default copy constructor is created for you when you don't specify one yourself. In such case, the default copy constructor will simply do a bitwise copy for primitives (including...
34
3057
by: Andy | last post by:
1) Is there any use of defining a class with a single constructor declared in private scope? I am not asking a about private copy constructors to always force pass/return by reference. 2) Is...
18
2970
by: Matt | last post by:
I try to compare the default constructor in Java and C++. In C++, a default constructor has one of the two meansings 1) a constructor has ZERO parameter Student() { //etc... } 2) a...
2
1079
by: Shlomy Shivek | last post by:
With C# I am able to do something like that: public ClassName() {some code here} public ClassName(Parameters here):this() {some code here} and when calling the parameters constructor it calls...
7
1843
by: David Lindauer | last post by:
If I do this: struct aa { int r,s,t; aa(); aa(const aa& t); }; int tt()
13
2361
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"); }
22
3587
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
5
2059
by: vairavans | last post by:
Hi Everyone, I have the following code, class B { }; class A {
0
7202
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7280
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,...
1
6991
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...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3167
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
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
380
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.