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 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 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
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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) {}
|
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?
|
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
|
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
|
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 ??
| |
by: David Lindauer |
last post by:
If I do this:
struct aa
{
int r,s,t;
aa();
aa(const aa& t);
};
int tt()
|
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");
}
|
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);
|
by: vairavans |
last post by:
Hi Everyone,
I have the following code,
class B
{
};
class A
{
|
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...
|
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,...
| |
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,...
|
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...
|
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...
|
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();...
|
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...
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |