By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,804 Members | 1,634 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,804 IT Pros & Developers. It's quick & easy.

Problem with constructors

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a

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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.