From: JyotiR on
Dear all,

In my program there is a constructure declared like:
Class Myclass{
public:
myclass(int n)
{
.....
.....
}
};

i'm calling my class like following:

Class myClass obj(1);
obj.DoModel() //DoModel is a MFC/C++ API

while debuging this code i got to know that the objct is being
initialize twice, thus calling constructure twice. I do not know why??
Can you pls tell me in which situation the object will be created
twice.Actully i want to initialize the object only once.
So, can you pls tell me what mistake i'm doing and for what reson the
object is being initialized twice??
Pls help!!
Thanks.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Ulrich Eckhardt on
JyotiR wrote:
> In my program there is a constructure declared like:
> Class Myclass{
> public:
> myclass(int n)
> {
> ....
> ....
> }
> };
>
> i'm calling my class like following:
>
> Class myClass obj(1);
> obj.DoModel() //DoModel is a MFC/C++ API

There is "Myclass", "myclass" and "Class" as names. I guess all three are
the same. In general, a minimal compilable example would be a good start
when demonstrating things.

> while debuging this code i got to know that the objct is being
> initialize twice, thus calling constructure twice.

I guess you rather have two constructor calls on two objects.

> Can you pls tell me in which situation the object will be created
> twice.

I can't, but you could tell us which line causes the first and which line
causes the second call. Note that it could also happen that your object is
copied, though that would call a different constructor. If you don't want
to support copying, you have to actively prohibit it, see the FAQ.

Uli

--
Sator Laser GmbH
Gesch�ftsf�hrer: Thorsten F�cking, Amtsgericht Hamburg HR B62 932


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Goran Pusic on
On Aug 4, 9:18 pm, JyotiR <rout.jyotiran...(a)gmail.com> wrote:
> Dear all,
>
> In my program there is a constructure declared like:
> Class Myclass{
> public:
> myclass(int n)
> {
> ....
> ....
>
> }
> };
>
> i'm calling my class like following:
>
> Class myClass obj(1);
> obj.DoModel() //DoModel is a MFC/C++ API
>
> while debuging this code i got to know that the objct is being
> initialize twice, thus calling constructure twice. I do not know why??
> Can you pls tell me in which situation the object will be created
> twice.Actully i want to initialize the object only once.
> So, can you pls tell me what mistake i'm doing and for what reson the
> object is being initialized twice??

I'll presume that the above should read:

myClass obj(1);
obj.DoModel(); // Not DoModal? OK...


If so, you are mistaken. I propose to put a breakpoint in the
constructor (Class::Class(int n)), debug and see where you are
constructing your object. One thing is certain, though, it's not being
constructed twice in what you've shown.

Goran.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Lailoken on
On Aug 4, 12:18 pm, JyotiR <rout.jyotiran...(a)gmail.com> wrote:
> Dear all,
>
> In my program there is a constructure declared like:
> Class Myclass{
> public:
> myclass(int n)
> {
> ....
> ....
>
> }
> };
>
> i'm calling my class like following:
>
> Class myClass obj(1);
> obj.DoModel() //DoModel is a MFC/C++ API
>
> while debuging this code i got to know that the objct is being
> initialize twice, thus calling constructure twice. I do not know why??
> Can you pls tell me in which situation the object will be created
> twice.Actully i want to initialize the object only once.
> So, can you pls tell me what mistake i'm doing and for what reson the
> object is being initialized twice??
> Pls help!!
> Thanks.

I have to agree with the other posters here, I can only assume what
your examples may refer to, but cannot help wonder that if you failed
to apply the same type of diligence to your code as you did to your
examples, then I would say that's the problem right there.

For instance... myclass(int n) cannot be the real constructor for the
class in question, but is this dues to your lack of effort on the
examples, or did you really use this all lowercase.

If this is indeed all lowercase then this constructor will not be
used, and if I can let my imagination wander then I would say that
perhaps some copy constructor is being called via some implicit
conversion from a temporary object? But then again, without a concrete
example I don't think anyone can help much.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]