Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / December 2004

Tip: Looking for answers? Try searching our database.

member method return type ?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
jmd - 14 Dec 2004 14:56 GMT
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.
Doug Harrison [MVP] - 14 Dec 2004 16:10 GMT
>hello.
>i am trying VC++ 7.1 (with vsnet 2003).
[quoted text clipped - 23 lines]
>
>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.

Signature

Doug Harrison
Microsoft MVP - Visual C++

jmd - 14 Dec 2004 17:32 GMT
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.

> >hello.
> >i am trying VC++ 7.1 (with vsnet 2003).
[quoted text clipped - 41 lines]
> an object of its class is basic_string::substr, which returns a new string
> that contains the substring.
Ronald Laeremans [MSFT] - 14 Dec 2004 18:55 GMT
You aren't trying to define a data member of the class type you are defining
in your example.

Ronald Laeremans
Visual C++

> Thanks.
> But what about my reference from the book
[quoted text clipped - 69 lines]
>> string
>> that contains the substring.
Doug Harrison [MVP] - 14 Dec 2004 20:16 GMT
>> 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

<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
[quoted text clipped - 14 lines]
>
>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.

Signature

Doug Harrison
Microsoft MVP - Visual C++

jmd - 14 Dec 2004 22:24 GMT
Ok.
Thank you very much, Doug and Ronald, for your explanations.
Now I "re-understand" these points.
jean-marie.

>>> All that's required to return an object by value is a copy ctor, and
>>> your
[quoted text clipped - 45 lines]
> complete,
> and everything about its object layout is known.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.