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. 5 1355
jmd wrote: 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::s ubstr, which returns a new string
that contains the substring.
--
Doug Harrison
Microsoft MVP - Visual C++
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.
"Doug Harrison [MVP]" <ds*@mvps.org > wrote in message
news:78******** *************** *********@4ax.c om... jmd wrote:
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
thistype !!!
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::s ubstr, which returns a new string that contains the substring.
-- Doug Harrison Microsoft MVP - Visual C++
You aren't trying to define a data member of the class type you are defining
in your example.
Ronald Laeremans
Visual C++
"jmd" <jm***********@ iesn.be> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. .. 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.
"Doug Harrison [MVP]" <ds*@mvps.org > wrote in message news:78******** *************** *********@4ax.c om... jmd wrote:
>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::s ubstr, which returns a new string that contains the substring.
-- Doug Harrison Microsoft MVP - Visual C++
jmd wrote: "Doug Harrison [MVP]" <ds*@mvps.org > wrote in message news:78******* *************** **********@4ax. com... 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 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.
--
Doug Harrison
Microsoft MVP - Visual C++
Ok.
Thank you very much, Doug and Ronald, for your explanations.
Now I "re-understand" these points.
jean-marie.
"Doug Harrison [MVP]" <ds*@mvps.org > wrote in message
news:kr******** *************** *********@4ax.c om... jmd wrote:
"Doug Harrison [MVP]" <ds*@mvps.org > wrote in message news:78****** *************** ***********@4ax .com... 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 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.
-- Doug Harrison Microsoft MVP - Visual C++ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: MacFly |
last post by:
Hi everyone,
HRESULT WINAPI DirectPlayMessageHandler( PVOID pvUserContext, DWORD
dwMessageId, PVOID pMsgBuffer)
I want that method to be class member method so it could have access to
class variables, but don't know how to do it ?
I still receive errors when I try to use it.
|
by: Adam Dziendziel |
last post by:
Hi all!
I'm writing a luabind/boost::python-like binding utility for a Squirrel
language to generating wrapper-functions for C++ classes and have a
problem with passing the pointer-to-member argument and deducing the
type of them.
I want to make a library which I can use like that:
class_<fs::path, sq_filesystem::ttag>(v, "path", no_base)
|
by: Richard Webb |
last post by:
Hi all,
I guess this is more of a design problem than a language problem, but
I'm confused either way! I have a class and it has a private data member
which is a struct. The size of the struct is what I would call relatively
large (about 1Mb). I have written methods for this class so that the struct
can be correctly filled with the corerct data and certain parts of the
struct can be extracted. But the problem I face is that I now have...
|
by: Noah Coad [MVP .NET/C#] |
last post by:
How do you make a member of a class mandatory to override with a _new_
definition? For example, when inheriting from
System.Collections.CollectionBase, you are required to implement certain
methods, such as public void Add(MyClass c).
How can I enforce the same behavior (of requiring to implement a member with
a new return type in an inherited class) in the master class (similar to the
CollectionBase)?
I have a class called...
|
by: Nels Olsen |
last post by:
Our company is rewriting our product in .NET. The old product is in PowerBuilder, which is heavy on Hungarian notation. We are approaching the time where we have to finalize naming conventions for everything publicly exposed in our API
There are two camps -- the Hungarian camp and the "Readable English" camp. I am in the latter. Like Microsoft's official recommendations for .NET, I have seen the light. Readability is what matters when...
| |
by: Christoph Boget |
last post by:
Let's take the following class:
class MyClass {
private int privateVar;
public int PublicVar {
get { return privateVar; }
}
public MyClass() {}
|
by: Jeff Grills |
last post by:
I am an experienced C++ programmer with over 12 years of development, and I
think I know C++ quite well. I'm changing jobs at the moment, and I have
about a month between leaving my last job and starting my new one. In that
time, I have decided to learn C#. I picked up the book, "Programming C#
(4th Edition)" recently and have read most of it. I've written about 1,500
lines of C# now, and I've run into the first really ugly thing I...
|
by: anthony.wolfe |
last post by:
Hello all,
I'm hoping that someone could help me with this bit of code. I am
using reflection to dynamically call a method within an HttpHandler.
When a method returns a user defined type that implements a certain
interface, I want to call that interfaces method. The problem is that
the local varaible holding the user defined type is declared an object.
The compiler will accept the invocation of the interface method on that
object. I...
|
by: Sambo |
last post by:
By accident I assigned int to a class member 'count' which was initialized to (empty) string and had no error till I tried to use it as string, obviously. Why was there no error on assignment( near the end ).
class Cgroup_info:
group_name = ""
count = "0" #last time checked and processed/retrieved
first = "0"
last = ""
retrieval_type = "" # allways , ask( if more than some limit), none
date_checked = ""
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |