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 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 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
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,...
|
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...
|
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...
|
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...
|
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...
| |
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...
|
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,...
|
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,...
| |
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...
|
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,...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |