member method return type ?

  • Thread starter Thread starter jmd
  • Start date Start date
J

jmd

hello.
i am trying VC++ 7.1 (with vsnet 2003).
my example :

class Complex
{
public:
Complex ( float re_ = 0.0, float im_ = 0.0 ) : re(re_), im(im_) {}
float Re () { return ( re ); }
float Re ( float re_ ) { return ( re = re_ ); }
float Im () { return ( im ); }
float Im ( float im_ ) { return ( im = im_ ); }

Complex operator+= ( Complex c ); // <=== problem or not ?

private:
float re, im;
};

the above code compiles & executes whithout errors (also Disable Language
Extensions set to YES).

it seems to me that a c++ method of a class could not return an objet of
that class, but only a reference (&) or pointer (this) to an object of this
type !!!

can you give me an explanation ?

thank you.

jean-marie.
 
jmd said:
hello.
i am trying VC++ 7.1 (with vsnet 2003).
my example :

class Complex
{
public:
Complex ( float re_ = 0.0, float im_ = 0.0 ) : re(re_), im(im_) {}
float Re () { return ( re ); }
float Re ( float re_ ) { return ( re = re_ ); }
float Im () { return ( im ); }
float Im ( float im_ ) { return ( im = im_ ); }

Complex operator+= ( Complex c ); // <=== problem or not ?

private:
float re, im;
};

the above code compiles & executes whithout errors (also Disable Language
Extensions set to YES).

it seems to me that a c++ method of a class could not return an objet of
that class, but only a reference (&) or pointer (this) to an object of this
type !!!

can you give me an explanation ?

All that's required to return an object by value is a copy ctor, and your
class Complex has an implicit one.

It's conventional for assignment operators, including compound assignment
operators like operator+=, to return *this by reference, but it isn't
required. Certainly, they can return the object by value, but it's not
advisable, because (1) it's unconventional, and (2) you make users pay for
creation and destruction of unnecessary temporaries, modulo the compiler's
ability to optimize them away. Assignment operators normally either return
*this by reference or they return void, the latter being used to defeat
chaining or simply out of brevity when declaring private assignment
operators that you never define, which is part of the "disable copying"
idiom.

An example of a C++ Standard Library non-static member function that returns
an object of its class is basic_string::substr, which returns a new string
that contains the substring.
 
Thanks.
But what about my reference from the book
C++ Primer third edition(1998) from Stanley B. Lippman & Josée Lajoie
At page 645 of this book, the author says with an example :

A nonstatic data member is restricted to being declared as a pointer or a
reference to an object of its class.
For example :
class Bar {
public:
// ...
private:
static Bar mem1; // ok
Bar *mem2; // ok
Bar mem3; // error
};

Where is the truth ?

jean-marie.
 
You aren't trying to define a data member of the class type you are defining
in your example.

Ronald Laeremans
Visual C++

jmd said:
Thanks.
But what about my reference from the book
C++ Primer third edition(1998) from Stanley B. Lippman & Josée Lajoie
At page 645 of this book, the author says with an example :

A nonstatic data member is restricted to being declared as a pointer or a
reference to an object of its class.
For example :
class Bar {
public:
// ...
private:
static Bar mem1; // ok
Bar *mem2; // ok
Bar mem3; // error
};

Where is the truth ?

jean-marie.
 

<snip>

Just in case it isn't clear, "returning an object by value" implies
returning a copy of the object.
Thanks.
But what about my reference from the book
C++ Primer third edition(1998) from Stanley B. Lippman & Josée Lajoie
At page 645 of this book, the author says with an example :

A nonstatic data member is restricted to being declared as a pointer or a
reference to an object of its class.
For example :
class Bar {
public:
// ...
private:
static Bar mem1; // ok
Bar *mem2; // ok
Bar mem3; // error
};

Where is the truth ?

As Ronald said, your example didn't do that. A class can't have a non-static
member variable of its own type, because that creates an infinite recursion
scenario. Above, mem3 would contain a Bar* (mem3.mem2) then another Bar
(mem3.mem3), and so on, forever and ever. On the other hand, it's no problem
for non-static member functions of a class X to return X objects or define X
parameters and local variables. You can think of the definitions of all the
member functions defined inside the class body being transported outside the
class to follow the class's definition, at which point the type is complete,
and everything about its object layout is known.
 
Ok.
Thank you very much, Doug and Ronald, for your explanations.
Now I "re-understand" these points.
jean-marie.
 
Back
Top