Topic: Initialization of function class args
Author: tgaunt@pms701.pms.ford.com (Ted Gaunt)
Date: Mon, 09 Jan 95 16:45:21 EST Raw View
IF I have a complex class Zclass which has a constructor which
may be initialized by another instance of Zclass.
Zclass a;
a=1;
Zclass *b=new Zclass(a);
and I have four functions which take Zclass as a paramater:
void function1(Zclass z1);
void function2(const Zclass z1);
void function3(const Zclass &z1);
void function4(Zclass &z1);
What is the difference in how the argument z1 is initialized
for usage in the function?
Which of the above functions are supposed to create a copy
and which are supposed to use the reference?
The problem that I am having is that I have a situation with
MS VC++ 1.5 where I have a function defined as
void function2(const Zclass z1);
and when I do:
Zclass a;
function2(a);
a=1;
then my debugger gives me information that appears to show that
a is passed to function2 as a reference, and therefore no
constructor is called. And when function2 is complete, the
destructor for parameter in function2 is called. Why should it
call the destructor when it was passed by reference!
So What Gives?
--
-----------------------------------
| Ted Gaunt |
| Ann-Arbor, Michigan, USA |
| tgaunt@pms701.pms.ford.com |
-----------------------------------
Author: rmartin@rcmcon.com (Robert Martin)
Date: Tue, 10 Jan 1995 18:24:02 GMT Raw View
tgaunt@pms701.pms.ford.com (Ted Gaunt) writes:
>IF I have a complex class Zclass which has a constructor which
>may be initialized by another instance of Zclass.
>Zclass a;
>a=1;
>Zclass *b=new Zclass(a);
>and I have four functions which take Zclass as a paramater:
>void function1(Zclass z1);
The compiler makes a copy of the argument prior to calling this constructor.
>void function2(const Zclass z1);
The compiler makes a copy of the argument prior to calling this constructor,
and then will not allow the constructor to change the copy. (useless?)
>void function3(const Zclass &z1);
The compiler passes a reference to the argument into the constructor and will
not allow the constructor to change the referenced object.
>void function4(Zclass &z1);
The compiler passes a reference to the argument into the constructor and will allow the constructor to change the referenced object.
>The problem that I am having is that I have a situation with
>MS VC++ 1.5 where I have a function defined as
> void function2(const Zclass z1);
>and when I do:
> Zclass a;
> function2(a);
> a=1;
>then my debugger gives me information that appears to show that
>a is passed to function2 as a reference, and therefore no
>constructor is called. And when function2 is complete, the
>destructor for parameter in function2 is called. Why should it
>call the destructor when it was passed by reference!
Check closer, perhaps your compiler is buggy. A copy of a should be
created prior to the call to function2. Upon return from function2,
that copy should be destroyed.
--
Robert Martin | Design Consulting | Training courses offered:
Object Mentor Assoc.| rmartin@rcmcon.com | Object Oriented Analysis
2080 Cranbrook Rd. | Tel: (708) 918-1004 | Object Oriented Design
Green Oaks IL 60048 | Fax: (708) 918-1023 | C++