Re: Problem with constructors

From: Peter MacMillan (peter_at_writeopen.com)
Date: 03/30/05


Date: Wed, 30 Mar 2005 03:29:37 -0500

manuthomas23@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: peter@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(--)