Problem with constructors

D

darkstorm

I have a problem with constructors.

Consider this:

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

void A::Fun()
{
Vector2D vec1;-------(B)
 
P

Peter MacMillan

darkstorm said:
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: (e-mail address removed)
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(--)
 
M

manuthomas23

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.
 
K

Karl Heinz Buchegger

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.
 
P

Peter MacMillan

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: (e-mail address removed)
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(--)
 
M

manuthomas23

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top