473,795 Members | 2,410 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Questing regarding returning of new objects

Hello everybody,

the question is, what is the "golden way" or the best way, if I have a
memberfunction in a class, which should return a new instance of an
object.
For example some class Foo which holds a lot data and has the
overloaded '+' operator and I want to do something like:

//f1-f3 are all Foo instances
f1 = f2+f3;

Is it in that case better to work with pointers in general like:

Foo* Foo::operator+( Foo& f){
//Do stuff
return new Foo([lot,of,data]);
}

to avoid the copying of the data which is hold by the new object at
the end of the function? Or is there any sensefull way to implement
that with a reference (Foo& Foo::operator+( Foo& f)), since it is more
intuitive?
Just imagine that the class Foo is somekind of Matrixclass with a big
2 dimensional array as member variable. Or is the only way just to
write Foo that it holds only a pointer to that big variable? Sure,
there are numerous solutions, but I would like to know which is the
most common and most elegant by some experienced C++ developers.

Thanks in advance,
Thorsten

Sep 9 '07 #1
6 1518
On 2007-09-09 12:58, th************* ***@gmail.com wrote:
Hello everybody,

the question is, what is the "golden way" or the best way, if I have a
memberfunction in a class, which should return a new instance of an
object.
For example some class Foo which holds a lot data and has the
overloaded '+' operator and I want to do something like:

//f1-f3 are all Foo instances
f1 = f2+f3;

Is it in that case better to work with pointers in general like:

Foo* Foo::operator+( Foo& f){
//Do stuff
return new Foo([lot,of,data]);
}
No, the + operator should return a new object to keep the semantics sane.
to avoid the copying of the data which is hold by the new object at
the end of the function? Or is there any sensefull way to implement
that with a reference (Foo& Foo::operator+( Foo& f)), since it is more
intuitive?
Just imagine that the class Foo is somekind of Matrixclass with a big
2 dimensional array as member variable. Or is the only way just to
write Foo that it holds only a pointer to that big variable? Sure,
there are numerous solutions, but I would like to know which is the
most common and most elegant by some experienced C++ developers.
There are two ways to solve this, the best but hardest to implement is
to use expression templates, unless you are writing a library that will
be used in many different applications this is probably too much work.

The other solution is to not use the + operator and instead use the +=
operator:

Foo f1(f2);
f1 += f3;

There are other solutions but they don't use the same syntax, such as
making a constructor that takes two Foo objects and an operator.

--
Erik Wikström
Sep 9 '07 #2
hi,

what's my query is what's the full neccessity for operator
overloading.Why we are adopting this concept.there is often lies a
complexity working with this concept.Can anyone explain what are all
the easy ways to getting the idea about Runtime POLYMORPHISM.

Sep 9 '07 #3
On 2007-09-09 14:58, vs********@gmai l.com wrote:
hi,

what's my query is what's the full neccessity for operator
overloading.Why we are adopting this concept.there is often lies a
complexity working with this concept.
There is no necessity in operator overloading, but by using it we can
simplify the *usage* of the types we design. And just as you say it does
add a little extra complexity when writing the code for the type, but
generally no more than using normal functions would. But the important
thing to remember is that is can greatly simplify the usage of classes,
just look at std::complex. You can use them more or less just like a
normal arithmetic type when you write expressions. Lets take a look at
what the code would look like if you do not have operators for the
following expression:

a = b + c * (d - e / f) * g;

Would be something like:

a = b.add(c.multipl y(d.minus(e.dev ide(f))).multip ly(g));

or

a = add(b, multiply(multip ly(c, minus(d, divide(e,f)) , g)));

I am not sure I got the second one correct, might have misplaced a
parenthesis, or perhaps even a whole sub-expression. Both of the
expressions that don't use operators take more to write, are harder to
tell what they do, and are harder to write.
Can anyone explain what are all the easy ways to getting the idea
about Runtime POLYMORPHISM.
You should start a new thread for this question, but performing a search
on google might be a better idea, or search the group archives.

--
Erik Wikström
Sep 9 '07 #4
you return the reference to "this", there is no performance penalty
then, as a reference as same as a pointer

MyClass & MyClass::operat or+=(const MyClass &rhs)
{
... // Do the compound assignment work.

return *this;
}

see some details here:
http://www.cs.caltech.edu/courses/cs...e/cpp-ops.html

Sep 9 '07 #5
th************* ***@gmail.com wrote:
the question is, what is the "golden way" or the best way, if I have a
memberfunction in a class, which should return a new instance of an
object.
For example some class Foo which holds a lot data and has the
overloaded '+' operator and I want to do something like:

//f1-f3 are all Foo instances
f1 = f2+f3;

Is it in that case better to work with pointers in general like:

Foo* Foo::operator+( Foo& f){
//Do stuff
return new Foo([lot,of,data]);
}

to avoid the copying of the data which is hold by the new object at
the end of the function? Or is there any sensefull way to implement
that with a reference (Foo& Foo::operator+( Foo& f)), since it is more
intuitive?
Just imagine that the class Foo is somekind of Matrixclass with a big
2 dimensional array as member variable. Or is the only way just to
write Foo that it holds only a pointer to that big variable? Sure,
there are numerous solutions, but I would like to know which is the
most common and most elegant by some experienced C++ developers.
You are concerned about the costs of copying a temporary object. There are
several things that help here:

a) A compiler can implement return value optimization. In the case of a
function like

Foo operator+ ( Foo const & lhs, Foo const & rhs ) {
Foo result ( something );
...
return ( result );
}

the standard permits that the result value be constructed in a place
convenient for the caller of operator+ so that copying the result is
avoided. Many compilers implement this. In this case, your question
interpreted in the narrowest scope becomes pointless. But a more general
concern about the copy costs of big objects remains.
b) The class can internally employ reference counting and make copying
cheap. I.e., you can implement Foo like so (untested, will be buggy):

class Foo {

tr1::shared_ptr < FooData the_data;

void ensure_unique ( void ) {
if ( ! the_data.unique () ) {
shared_ptr< FooData new_data ( new FooData ( *the_data ) );
the_data.swap( new_data );
}
}

public:

...

void some_const_meth od ( some_type some_par ) const {
the_data->some_const_met hod( some_par );
}

void some_non_const_ method ( some_type some_par ) {
ensure_unique() ;
the_data->some_non_const _method( some_par );
}

};

Now, copying is cheap, but you add a little overhead just about everywhere
else. Also note that non-const members may have to create their private
copy of the data anyhow.
c) Expression templates can be used to trade the creation of costly
temporaries for creation of cheap temporaries. This technique is employed
in some publicly available matrix classes. Expression templates allow you
to reduce the number of costly objects created by postponing the actual
creation of costly objects to the very last moment (at which point some
temporaries have been bypassed). You will find plenty of information about
this in the archives.
BTW: if your Foo really is a matrix class, I strongly suggest using one of
the classes around. It is rather tricky to get a matrix class Right(tm).
Best

Kai-Uwe Bux
Sep 9 '07 #6
On Sep 9, 1:23 pm, Erik Wikström <Erik-wikst...@telia. comwrote:
On 2007-09-09 12:58, thorsten.schill ...@gmail.com wrote:
the question is, what is the "golden way" or the best way,
if I have a memberfunction in a class, which should return a
new instance of an object. For example some class Foo which
holds a lot data and has the overloaded '+' operator and I
want to do something like:
//f1-f3 are all Foo instances
f1 = f2+f3;
Is it in that case better to work with pointers in general like:
Foo* Foo::operator+( Foo& f){
//Do stuff
return new Foo([lot,of,data]);
}
No, the + operator should return a new object to keep the
semantics sane.
100% agreed. The semantics of the final program must always be
as if operator+ returned a new object. And not a new'ed object;
it's almost impossible to use an operator+ which returns a
new'ed object without leaking memory.

Of course, the the returned object doesn't have to be of type
Foo. A frequent solution is to return a proxy of some sort.
to avoid the copying of the data which is hold by the new
object at the end of the function? Or is there any sensefull
way to implement that with a reference (Foo&
Foo::operator+( Foo& f)), since it is more intuitive? Just
imagine that the class Foo is somekind of Matrixclass with a
big 2 dimensional array as member variable. Or is the only
way just to write Foo that it holds only a pointer to that
big variable? Sure, there are numerous solutions, but I
would like to know which is the most common and most elegant
by some experienced C++ developers.
There are two ways to solve this, the best but hardest to
implement is to use expression templates, unless you are
writing a library that will be used in many different
applications this is probably too much work.
It's actually not that much work, total. It's a lot of extra
code, but it's grunt code, which doesn't require much thought,
once the design has been decided on. (I've not tried, since I
haven't needed such in my applications, but I'll bet you could
write a simple script to generate it: you specify the base type
and the desired operators, and the script generates the rest.)

Note too that you don't really need templates for this;
inheritance works just as well. The technique was being used
long before C++ compilers supported templates. (It's a curious
case of inheritance, since the virtual functions are all inline,
and all of the objects of the derived class are normally
temporaries, generated by the compiler on the stack, and whose
dynamic type is known to the compiler, so it doesn't have to
generate the usual code for a virtual function call.)
The other solution is to not use the + operator and instead
use the += operator:
Foo f1(f2);
f1 += f3;
That puts the onus on the user, and is a real pain for more
complicated expressions (which require explicit temporaries).

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Sep 10 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2242
by: akash shetty | last post by:
hi, im developing a code which requires searching a large database(bioological) for certain patterns.the size of the file is 3.5GB . the search pattern is a ten letter string.the database consists of paragraphs. the code ive developed searches the data paragraphwise. (using xreadlines). but this takes an awful amt of time.(abt 7 mins) is there anyway to speed this up.
18
2146
by: cppaddict | last post by:
Hi, Is it considered bad form to have the subscript operator return a const reference variable? If not, what is the proper way to do it? My question was prompted by the code below, my problematic attempt to implement a subscript operator that returns a const reference. The dubious code is marked at the end. <code>
12
3294
by: Olumide | last post by:
I'm studying Nigel Chapman's Late Night Guide to C++ which I think is an absolutely fantastic book; however on page 175 (topic: operator overlaoding), there the following code snippet: inline MFVec operator+(const MFVec& z1, const MFVec& z2) // Global function { MFVec res = z1; res += z2 return res; // WHY???
8
2090
by: Derek | last post by:
Some authors advocate returning const objects: const Point operator+(const Point&, const Point&); ^^^^^ Returning a const object prevents some bad code from compiling: Point a, b, c; (a + b) = c; // error
8
3328
by: gg | last post by:
I am confused regarding what the line in the following function does. It seems to work ok. It seems to be creating a new T object using the pointer to an existing T object. But which function is it calling to create the new T object? void f ( auto_ptr < T > & aPT ) { auto_ptr < T > newPT ( new T ( aPT.get ( ) ) ); }
1
2801
by: trialproduct2004 | last post by:
Hi all, I am having slight confusion regarding memory management in .net. Say suppose i have two application one is in C# and other is in MFC(VC++). Both of this application are using lots of memory. Suppose i run first C# application which has occupied all memory and
8
2346
by: Myron Marston | last post by:
For a WebService I am developing, I return a DataSet that contains a table listing Field Names and Field Values. The FieldValue column is given the type object becuase it can potentially contain any of the primative value types (date, int, decimal, string, bool, etc). However, when my client consumes the webservice, it is always a string, even if the value wasn't a string when it was put into the dataset. How do I return different types...
2
1336
by: dejavue82 | last post by:
Dear ASP.net programmers, I was wondering about the following: 1) Do hosts automatically support all of the .NET framework? (ex. System.Image) 2) If one rents a dedicated server, does one have full control over it? (ie. one can install any 3rd part component such a ASP Turbine 7)
7
2874
by: Thomas Lenz | last post by:
Please consider the following code snippet: string myfunction() { ostringstream oss; oss << "junk"; // do something more with oss; I can't make it const... return oss.str(); } Is the returned string object still valid after myfunction() has returned? I
0
9672
marktang
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...
0
10437
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10214
jinu1996
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...
1
10164
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,...
0
9042
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6780
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();...
0
5437
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...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3723
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.