473,387 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

void operator=

Per
Hi!
I have a question about the operator=
I have seen two versions:

1.
void operator= (const SomeClass& C) {
....
}

2.
SomeClass& operator= (const SomeClass& C) {
....
return *this;
}

What is the difference and do one gain any performance using version
1. Is version 1 working with STL?
Jul 22 '05 #1
4 16419
"Per" <pe*********@gmail.com> wrote in message
news:f6**************************@posting.google.c om...
Hi!
I have a question about the operator=
I have seen two versions:

1.
void operator= (const SomeClass& C) {
....
}

2.
SomeClass& operator= (const SomeClass& C) {
....
return *this;
} This is the correct form, equivalent to the built-in versions
of the operator.
See "Effective C++" item 15.
What is the difference and do one gain any performance
using version 1. A measurable gain is very unlikely, and it is even quite
possible that there would be no gain at all in code size
or execution speed.
Is version 1 working with STL?


It is not guaranteed to.
An STL implementation can legally assign objects as follows:
a = b = c;

hth,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com
Jul 22 '05 #2
In the first case you can't do:
SomeClass a,b,c;
...
a=b=c;
In the second case you can!

Jul 22 '05 #3
Per wrote:
What is the difference and do one gain any performance using version
1. Is version 1 working with STL?


Both are legal and probably have the same performance.
The problem with #1 is that it does't emulate the builtin operator=
which is generally regarded as bad style.

SomeClass a, b, c;
a = b = c; // won't compile with a void returning op=
Jul 22 '05 #4
Yeh, like the others already stated, the performance gain will be
minimal to non-existend - depending on the compiler and whether it
inlines the function or not.
Therefor no reason I can see not to return the ref.
Jul 22 '05 #5

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

Similar topics

24
by: Marcin Vorbrodt | last post by:
Is there any reason why auto_ptr does not have the cast operator like this: operator void* (); So that one could easily test auto_ptr for NULL ??? Standard does not declare such operator. Is...
2
by: Jakob Bieling | last post by:
Hi, I remember reading a document that advised to prefer 'operator void*' over 'operator bool' or other way round, when I want to provide the ability to use code like this: test_class t;...
18
by: Marcin Kalicinski | last post by:
Hi, Why is there void* conversion to check for std::istream failure bit? Why not a conversion to bool? When I try to return an istream from a function that actually returns bool, I get the...
14
by: deanbrown3d | last post by:
Hi there! Suppose I have a function void ShowMessage(string s); (that has a void return type.) In my other function F, also with a void return type, can I do this?
188
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
29
by: whatluo | last post by:
Hi, c.l.cs I noticed that someone like add (void) before the printf call, like: (void) printf("Timeout\n"); while the others does't. So can someone tell me whether there any gains in adding...
56
by: maadhuu | last post by:
hello, this is a piece of code ,which is giving an error. #include<stdio.h> int main() { int a =10; void *p = &a; printf("%d ", *p ); //error....why should it //be an error ?can't the...
6
by: Angus | last post by:
I am using a global which is a void* I have it defined in one file as: void* hFlag; and one other header file as: extern void* hFlag; But I get this compile error:
15
by: Dan Henry | last post by:
I have run across functions in the Linux kernel's MTD driver that have me scratching my head a bit. The functions have the general form: extern int bar(size_t len, size_t *retlen, unsigned char...
16
by: PeterAPIIT | last post by:
Hello all C++ expert programmer, i have wrote partial general allocator for my container. After reading standard C++ library and code guru article, i have several questions. 1. Why...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...

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.