473,406 Members | 2,273 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,406 software developers and data experts.

operator= not called?

Hello, everyone, could you make me know why operator= is not called in
this code?
Thank you.
class cls
{
public:
cls()
{
b=true;
i=100;
d=88;
}

bool b;
int i;
double d;

cls& operator= (const cls& rhs);

};

cls& cls::operator= (const cls& rhs)
{
i=rhs.i;
return * this;
}

int _tmain(int argc, _TCHAR* argv[])
{
cls a;
a.i=1000;
a.b=false;
a.d=55;

cls b=a;
return 0;
}

Nov 22 '06 #1
6 1551

deerc...@gmail.com skrev:
Hello, everyone, could you make me know why operator= is not called in
this code?
Thank you.
class cls
{
public:
cls()
{
b=true;
i=100;
d=88;
}

bool b;
int i;
double d;

cls& operator= (const cls& rhs);

};

cls& cls::operator= (const cls& rhs)
{
i=rhs.i;
return * this;
}

int _tmain(int argc, _TCHAR* argv[])
{
cls a;
a.i=1000;
a.b=false;
a.d=55;

cls b=a;
The compiler is allowed to optimise this as cls b(a) - using copy
construction. This is perfectly legal.

/Peter

Nov 22 '06 #2
de******@gmail.com wrote:
Hello, everyone, could you make me know why operator= is not called in
this code?
[..]
cls b=a;
This is a definition/initialisation syntax. It has nothing to do
with operator=. They both just use the same symbol ('='). If you
wanted to assign, you needed to write

cls b;
b=a;

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 22 '06 #3
peter koch wrote:
deerc...@gmail.com skrev:
>Hello, everyone, could you make me know why operator= is not called
in this code?
Thank you.

[..]
cls b=a;

The compiler is allowed to optimise this as cls b(a) - using copy
construction. This is perfectly legal.
Even if it doesn't, there is no operator= involvement here. The
statement is semantically equivalent to

cls b=cls(a);

which is the same as

cls b((cls(a)));

which is still just copy construction.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 22 '06 #4

"peter koch дµÀ£º
"
The compiler is allowed to optimise this as cls b(a) - using copy
construction. This is perfectly legal.

/Peter
Thanks, you're right.

Nov 22 '06 #5

Victor Bazarov skrev:
peter koch wrote:
deerc...@gmail.com skrev:
Hello, everyone, could you make me know why operator= is not called
in this code?
Thank you.

[..]
cls b=a;
The compiler is allowed to optimise this as cls b(a) - using copy
construction. This is perfectly legal.

Even if it doesn't, there is no operator= involvement here. The
statement is semantically equivalent to

cls b=cls(a);

which is the same as

cls b((cls(a)));

which is still just copy construction.
Okay - my fault. I was under the impression that operator= had to be
present and that the compiler was allowed to default construct and use
that operator.

/Peter

Nov 22 '06 #6

"Victor Bazarov дµÀ£º
This is a definition/initialisation syntax. It has nothing to do
with operator=. They both just use the same symbol ('='). If you
wanted to assign, you needed to write

cls b;
b=a;
Great, now I know it. Thank you guys.

Nov 22 '06 #7

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

Similar topics

15
by: Steve | last post by:
Hi, I hope someone can help. I have a class called cField, and another class called cFieldList. cFieldList contains a std::vector of cFields called myvec I've overloaded the subscript...
20
by: Ioannis Vranos | last post by:
When we use the standard placement new operator provided in <new>, and not a definition of owr own, isn't a call to placement delete enough? Consider the code: #include <new>
3
by: Alicia | last post by:
Hello, I am trying to figure out how to call an overloaded operator<< inherited from a base class. #ifndef PHONECALL #define PHONECALL #include "time.h" #include "interval.h"
15
by: Heiner | last post by:
#include <stdio.h> class A { public: virtual A & operator= (const A &); virtual void test(const A &); }; class B : public A
6
by: Lighter | last post by:
Big Problem! How to overload operator delete? According to C++ standard, "A deallocation function can have more than one parameter."(see 3.7.3.2); however, I don't know how to use an overloaded...
5
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
5
by: raan | last post by:
What I am trying to achieve here is depicted in the small program below. // Wrapit.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <map> #include...
2
by: sven.bauer | last post by:
Hi, I have a question following up the following slightly older posting: http://groups.google.de/group/comp.lang.c++/browse_thread/thread/40e52371e89806ae/52a3a6551f84d38b class Base {...
10
by: siddhu | last post by:
Dear Experts, I want to make a class whose objects can be created only on heap. I used the following approach. class ss { ss(){} public: void* operator new(size_t sz)
3
by: Martin T. | last post by:
Hello. I tried to overload the operator<< for implicit printing of wchar_t string on a char stream. Normally using it on a ostream will succeed as std::operator<<<std::char_traits<char> will...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
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...
0
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,...

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.