operator= not called?

D

deerchao

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::eek:perator= (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;
}
 
P

peter koch

(e-mail address removed) 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::eek:perator= (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
 
V

Victor Bazarov

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
 
V

Victor Bazarov

peter said:
(e-mail address removed) 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
 
D

deerchao

"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.
 
P

peter koch

Victor Bazarov skrev:
peter said:
(e-mail address removed) 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
 
D

deerchao

"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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top