Hi,
------------code--------------------------------------
#include<iostream>
using namespace std;
class B{
int y;
public:
B(int b=3):y(b){}
int getInt(){
return y;
}
};
class A{
int x;
public:
A(const int &x_){ //Line 1
x=x_;
}
A(B *b){
A(b->getInt()); //Line 2
}
int getInt(){
return x;
}
};
int main(){
B *b = new B;
A *a = new A(b);
cout<<a->getInt()<<endl;
}
---------------------------------------------------------
I expected that in line 2, "b->getInt()" is 3, and by calling
construction function of Line 1, the printed result should be 2;
But it's 0, can anyone tell me the reason? Thanks in advance. 5 1444
On Oct 1, 4:19*pm, thomas <FreshTho...@gmail.comwrote:
Hi,
------------code--------------------------------------
#include<iostream>
using namespace std;
class B{
* * * * int y;
public:
* * * * B(int b=3):y(b){}
* * * * int getInt(){
* * * * * * * * return y;
* * * * }
};
class A{
* * * * int x;
public:
* * * * A(const int &x_){ * * * * *//Line 1
* * * * * * * * x=x_;
* * * * }
* * * * A(B *b){
* * * * * * * * A(b->getInt()); * * * *//Line *2
* * * * }
* * * * int getInt(){
* * * * * * * * return x;
* * * * }
};
int main(){
* * * * B *b = new B;
* * * * A *a = new A(b);
* * * * cout<<a->getInt()<<endl;}
---------------------------------------------------------
I expected that in line 2, "b->getInt()" is 3, and by calling
construction function of Line 1, the printed result should be 2;
But it's 0, can anyone tell me the reason? Thanks in advance.
ok, got it, cannot call constructor in a constructor.
On Oct 1, 4:19*pm, thomas <FreshTho...@gmail.comwrote:
* * * * A(B *b){
* * * * * * * * A(b->getInt()); * * * *//Line *2
* * * * }
---------------------------------------------------------
I expected that in line 2, "b->getInt()" is 3, and by calling
construction function of Line 1, the printed result should be 2;
But it's 0, can anyone tell me the reason? Thanks in advance.
This is because in Line 2, x member of the current object is not being
initialized, instead a new A object is created and it's x is
initialized. You need to simply initialize x of the current object.
A(B *b)
{
// probably have a check to see that b is not NULL before
accessing b ptr
x = b->getInt();
}
Thanks and regards
Sonison James
ok, got it, cannot call constructor in a constructor.
sure you can, in your case, as someone already mention, you're
creating a new object.
Here is how you can call another ctor
class A{
A(){}
A():this(){
}
};
On Oct 1, 11:03 am, Obnoxious User <O...@127.0.0.1wrote:
On Wed, 01 Oct 2008 06:07:10 -0700, puzzlecracker wrote:
ok, got it, cannot call constructor in a constructor.
sure you can, in your case, as someone already mention, you're creating
a new object.
Here is how you can call another ctor
class A{
A(){}
A():this(){
}
};
No you can't.
http://www.parashift.com/c++-faq-lit....html#faq-10.3
--
OU
Remember 18th of June 2008, Democracy died that afternoon.http://frapedia.se/wiki/Information_in_English
Opps, I was referring to calling the base class ctor. Sorry! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: tuko |
last post by:
Hello kind people.
Can someone explain please the following code?
/* Create Storage Space For The Texture */
AUX_RGBImageRec *TextureImage; /* Line 1*/
/* Set The Pointer To NULL...
|
by: Petter Reinholdtsen |
last post by:
Is the code fragment 'char a = ("a");' valid ANSI C? The
problematic part is '("a")'. I am sure 'char a = "a";' is valid
ANSI C, but I am more unsure if it is allowed to place () around the...
|
by: Adriano Coser |
last post by:
Hello.
I've just finished the compilation of my application with VC 2005 Beta 2
(Express edition) and I'm getting the following initialization error loading
one of my DLLs:
LDR:...
|
by: mj |
last post by:
When I'm trying to run any asp.net application, the following error appears:
"CS0007: Unexpected common language runtime initialization
error --Catastrophic failure"
I've reinstalled .NET...
|
by: Russ Barrett |
last post by:
Hello, I'm new to ASP.Net and I'm trying to interface an ASP.Net (C#)
application to functionality resident in a C++ static lib.
From posts I've seen around I understand this requires creation of a...
| |
by: Dilip |
last post by:
The subject is a bit misleading because I really can't figure out what
the following code snippet is doing and would appreciate any help in
deciphering it.
I mean I can understand code-wise what...
|
by: Jess |
last post by:
Hello,
I understand the default-initialization happens if we don't initialize
an object explicitly. I think for an object of a class type, the
value is determined by the constructor, and for...
|
by: Jess |
last post by:
Hello,
I tried several books to find out the details of object
initialization. Unfortunately, I'm still confused by two specific
concepts, namely default-initialization and...
|
by: subramanian100in |
last post by:
Consider the following program:
#include <iostream>
#include <string>
using namespace std;
class Test
{
public:
|
by: subramanian100in |
last post by:
Suppose we have a class named Test.
Test obj; // assuming default ctor is available
Test direct_init(obj); // direct initialization happens here
Test copy_init = obj; // copy initialization...
|
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: 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...
|
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...
|
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 ...
| |