473,654 Members | 3,104 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 1737

ma*********@REM OVETHISTOMAILME NOT.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.c om wrote:
ma*********@REM OVETHISTOMAILME NOT.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
9456
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, int b=1, int c=1) : _a(a), _b(b), _c(c) {}
15
21186
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 pointers) and for objects types call their default constructor. Any others points i should know?
34
3083
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 this in any way used to create singletons. Can someone say how? Cheers, Andy
18
2992
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 constructor that all parameters have default values
2
1088
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 the default constructor first and than continue to the parameter constructor code. Is it possible to do the same thing with vb.net ??
7
1848
by: David Lindauer | last post by:
If I do this: struct aa { int r,s,t; aa(); aa(const aa& t); }; int tt()
13
2372
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
3606
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 v);
5
2069
by: vairavans | last post by:
Hi Everyone, I have the following code, class B { }; class A {
0
8379
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8294
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8494
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8596
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5627
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4150
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2719
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 we have to send another system
2
1924
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.