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

Home Posts Topics Members FAQ

Problem with constructors

I have a problem with constructors.

Consider this:

class A
{
public:
void Fun();
private:
Vector2D vec;--------(A)
};

void A::Fun()
{
Vector2D vec1;-------(B)
Jul 23 '05 #1
5 1060
darkstorm wrote:
class A
{
public:
void Fun();
private:
Vector2D vec;--------(A)
};
This is just a definition of the class. When you instantiate the class
(eg. dynamically: A* a = ew A(); statically: A a;) it will create vec
and call it's constructor as part of the class initialization.


void A::Fun()
{
Vector2D vec1;-------(B)
.
.
.
}
When your execution gets into that function, a Vector2D named vec1 is
created (and destroied at the end of the function).

Here at (B) constructor for Vector2D is getting called, but at (A)
constructor is not getting called. What can be the reason?

Thanks


B is part of some code, A is part of a definition of a class.
--
Peter MacMillan
e-mail/msn: pe***@writeopen.com
icq: 1-874-927

GCS/IT/L d-(-)>-pu s():(-) a- C+++(++++)>$ UL>$ P++ L+ E-(-) W++(+++)>$
N o w++>$ O !M- V PS PE Y+ t++ 5 X R* tv- b++(+) DI D+(++)>$ G e++ h r--
y(--)
Jul 23 '05 #2

Actually I am creating an instance of A(say, A a) and calling the
function a.Fun(); But only at point (B) the constructor for Vector2D is
getting called.

Jul 23 '05 #3
ma**********@hotmail.com wrote:

Actually I am creating an instance of A(say, A a) and calling the
function a.Fun(); But only at point (B) the constructor for Vector2D is
getting called.


Then please show a complete, compileable, ready to run program.
If you are kind, you make it a small program.
Unless you do that, all we can do is guess what you did wrong.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #4
ma**********@hotmail.com wrote:
Actually I am creating an instance of A(say, A a) and calling the
function a.Fun(); But only at point (B) the constructor for Vector2D is
getting called.


Unless there's something unique about your Vector2D class (eg. a private
constructor), it is being called at both points. See the example below.

How are you determining that the ctor _isn't_ being called, btw? If
you've overloaded the constructor (say one that is parameterized), you
could be overlooking something in there (but I don't know because I
don't know what your Vector2D looks like).

//--
#include <iostream>

class X {
public: X() { std::cout << "X::X()" << std::endl; }
};

class Y {
public:
Y() { std::cout << "Y::Y()" << std::endl; }
void f();
private:
X x;
};

void Y::f() {
X x; //A
}

int main() {
Y y; //B
y.f();
}

/*
output:
X::X()
Y::Y()
X::X()
the first two lines of output start from B
the last line comes from A
*/
//--
--
Peter MacMillan
e-mail/msn: pe***@writeopen.com
icq: 1-874-927

GCS/IT/L d-(-)>-pu s():(-) a- C+++(++++)>$ UL>$ P++ L+ E-(-) W++(+++)>$
N o w++>$ O !M- V PS PE Y+ t++ 5 X R* tv- b++(+) DI D+(++)>$ G e++ h r--
y(--)
Jul 23 '05 #5
Sorry for troubling you all....I am working on Brew for Mobile games.
Here the base object is getting intialized internally. I am not
explicitly specifying the object. So its default constructor will not
be called....
Thanks for all the replies

Jul 23 '05 #6

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

Similar topics

42
5720
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
7
1613
by: Christian Engström | last post by:
When i compile the program listed below with gcc version 3.3.1 (MinGW on Windows XP) I get the following result: Calling 'func(d)': 'base' copy constructor Calling 'func(*d_handle)': 'base'...
4
1382
by: Gama Franco | last post by:
Hi, I've been developing this API, but now I get stuck in a compiling error and I'm out of ideas. Some comments are welcome. The hierarchy is bases in three classes, and I will explain it...
3
1724
by: rahul8143 | last post by:
hello, I write a following program and have problem in understanding constructors and destructors. #include <iostream.h> class vector { public: double x; double y;
6
2377
by: Stephen Martinelli | last post by:
thanks for the help...just one more question.... can a class have more then two parameterized constructors?..i would like to be able to instanciate the class with a different number of...
10
1731
by: John | last post by:
Trying to find out what is essential / optional, I made an extremely simple Class and Module combination to add two numbers. (see below) It appears that an empty constructor is needed n order to...
3
1627
by: John | last post by:
Before anything else, thanks Marina, Workgroups and Ralf, for your help so far. I am now able to better define the question! After adding more console printout lines to CSum, I tried all...
22
5171
by: Peter Morris [Droopy eyes software] | last post by:
Look at these two classes public class Test { public readonly string Name; public Test(string name)
2
1645
by: shuisheng | last post by:
Dear All, I have a question on inheritance. Assume I have a base class Vector and its derived class DecoratedVector as follows: class Vector { // Constructors. Vector(); Vector(size_t num,...
1
1719
by: connor7777 | last post by:
Hi guys: We've been weeding out errors off of a java->c# project and have managed to redeem most of our code with the exception of one bug that we for some reason cannot pin down. The following...
0
7278
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,...
0
7458
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...
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,...
1
5013
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...
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.