new class to class conversion question

J

John Cho

Class Cartesian
{ double x;
double y;
public:
Cartesian( ) { x = 0.0 ; y = 0.0;}
Cartesian(double x1, double y1)
{ x = x1; y = y1;}
};
class Polar
{ double radius;
double angle;
public:
Polar( ) { radius = 0.0; angle = 0.0;}
Polar(double r, double a) { radius = r; angle = a;}
//Use operator conversion function to convert Polar to Cartesian
operator Cartesian( )
{ double xx, yy;
xx = radius * cos(angle);
yy = radius * sin(angle);
return Cartesian(xx, yy);
}
};


that operator Cartesian, converts class Polar to Cartesian

via


Polar P;
Cartesian C;
C = P;


i am wondering about the
return Cartesian(xx, yy);
why is it that and not
return (Cartesian c2(xx, yy));

why is it just returning a constructor? that just does not make sense to
me
 
?

=?iso-8859-1?Q?Juli=E1n?= Albo

John Cho escribió:
i am wondering about the
return Cartesian(xx, yy);
why is it that and not
return (Cartesian c2(xx, yy));

Because in order to create a temporary, there is not need to give it a
name. The syntax used builds a temporary object and returns it.

Regards.
 
C

Clark Cox

John Cho said:
i am wondering about the
return Cartesian(xx, yy);
why is it that and not
return (Cartesian c2(xx, yy));

why is it just returning a constructor? that just does not make sense to
me

It's not actually returning the constructor. When that syntax is
used, it basically means "construct a temporary object, passing these
parameters to the constructor".
 
A

Andrey Tarasevich

John said:
...
i am wondering about the
return Cartesian(xx, yy);
why is it that and not
return (Cartesian c2(xx, yy));

why is it just returning a constructor? that just does not make sense to
me
...

Firstly, 'return (Cartesian c2(xx, yy))' is not a legal statement in
C++. And there's no such thing as "returning a constructor" in this
example or anywhere in C++.

Secondly, if you want to construct a named object and then return it you
have to do it this way

Cartesian c2(xx, yy);
return c2;

However, in this situation we don't care to have a named object. There's
simply no need for it. Instead, we can construct a temporary (anonymous)
object of type 'Cartesian' and return it right away. The temporary
object is created by the following expression

Cartesian(xx, yy)

so the entire statement that return such an object will look as follows

return Cartesian(xx, yy);
 
L

Leor Zolman

Class Cartesian
{ double x;
double y;
public:
Cartesian( ) { x = 0.0 ; y = 0.0;}
Cartesian(double x1, double y1)
{ x = x1; y = y1;}
};
class Polar
{ double radius;
double angle;
public:
Polar( ) { radius = 0.0; angle = 0.0;}
Polar(double r, double a) { radius = r; angle = a;}
//Use operator conversion function to convert Polar to Cartesian
operator Cartesian( )
{ double xx, yy;
xx = radius * cos(angle);
yy = radius * sin(angle);
return Cartesian(xx, yy);
}
};


that operator Cartesian, converts class Polar to Cartesian

via


Polar P;
Cartesian C;
C = P;


i am wondering about the
return Cartesian(xx, yy);
why is it that and not
return (Cartesian c2(xx, yy));

why is it just returning a constructor? that just does not make sense to
me

Okay. I'll skip the nits and get right to your question ;-)

It is not useful, in C++, to think about anything you write as a "call to a
constructor". You define objects:
Cartesian c;
says "define and default-initialize an object named c of type Cartesian";
in the process of doing that, the default constructor for Cartesian will be
invoked. But you're not "calling it directly", nor are you ever doing
anything with constructors, be it calling, returning, whatever.

Thus, given:
Cartesian c = Cartesian();
It says to instantiate an anonymous temporary object of type Cartesian
(using the default ctor), and then to instantiate an object named c and
copy the temporary object's value into c. A decent compiler may optimize
that so the resulting code is the same as just saying
Cartesian c;
but that's the idea.

The line you're asking about:
return Cartesian(xx,yy);
says to instantiate an anonymous object of type Cartesian (employing the
2-arg constructor), and then return a copy of that value as the return
value of the function (again, inlining and the "return value optimization"
may all contribute to slimming down the resulting code, but those are the
semantics.)

On the other hand,
return (Cartesian c2(xx, yy));

is a syntax error, because you're putting a definition of a named object
where an expression is required, and you can't do that. You could have
said:
Cartesian c2(xx, yy);
return c2;
however, and that would have been fine. But the "best" way to do it is
simply
return Cartesian(xx,yy);
because it gives even stupid (i.e. non-optimizing) compilers the best
chance of producing decent code.

HTH,
-leor





Leor Zolman
BD Software
(e-mail address removed)
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top