implicit copy assignment operator

G

George

Dear C++ Afficionados,

I know the compiler will generate a copy assignment operator (amongst
other fn's) if you don't provide one in your class declaration.

But the parameter types can vary, and the compiler decides between
const, reference, and (with xlC) volatile.

Is there any way to dump the object file and see what the compiler
generated? I couldn't see any switches for this. It would be helpful
to see I think.

Thanks much. Sincerely, George
 
A

athresh

George said:
Dear C++ Afficionados,

I know the compiler will generate a copy assignment operator (amongst
other fn's) if you don't provide one in your class declaration.

But the parameter types can vary, and the compiler decides between
const, reference, and (with xlC) volatile.

Is there any way to dump the object file and see what the compiler
generated? I couldn't see any switches for this. It would be helpful
to see I think.

Thanks much. Sincerely, George
 
E

E. Robert Tisdale

George said:
I know [that] the compiler will generate a copy assignment operator
(among other functions)
if you don't provide one in your class declaration.

No. For class X, yhe compiler will supply

1. an assignment operator,

X& X::eek:perator=(const X&);

2. a *default* constructor

X::X(void);

3. ans a copy constructor

X::X(const X&);

by default.
But the parameter types can vary,

If you need an assignment operator or default constructor
which accepts any arguments other that those above,
you must define them yourself.
and the compiler decides between
const, reference, and (with xlC) volatile.

Is there any way to dump the object file
and see what the compiler generated?

The typical compiler
never actually creates callable functions for them
but inlines them automatically.
 
G

George

I did,

T& operator=(const T&)

But the compiler wouldn't take it. So after several attempts, it was
happy without the const and the &.

There are 5 possible signatures, (at least with xlC). I suppose the
best solution is to define all 5.

-g
 
R

Richard Herring

In message said:
I did,

T& operator=(const T&)

But the compiler wouldn't take it.

?? It should take whatever you give it.

Post actual code, and the compiler's error messages.
So after several attempts, it was
happy without the const and the &.

Which would imply taking a copy before you perform the assignment.
There are 5 possible signatures, (at least with xlC). I suppose the
best solution is to define all 5.
No. The best solution is to say what you mean.
 
G

George

Oops, my bad. "An rvalue cannot be converted to a reference to a
non-const type."

I'd overlooked that compiler message.

Thanks for all the help. Sincerely,

George

---------------------------------------------------------------------
// This fails to compile, messages are:
//
// The call does not match any parameter list for "operator="
// "P::eek:perator=(P&)" is not a viable candidate.
// A parameter of type "P&" cannot e initalized with an rvalue of type
"P"
// An rvalue cannot be converted to a reference to a non-const type
//
// Compile with xlC -c <thisfile>

class P{
public:

explicit P(int i);
P(P& p);
P& operator=(P&);
void swishswish();
private:
int data;
};
inline P::p(int i) : data(i){ }
inline P::p(P& p):data(p.data) { }
inline P& P::eek:perator=(P& p){ data = p.data; return *this;}
void P::swishswish(){ data = 89; }

int main()
{
P iP(2);
iP = static_cast<P>(3);
iP.swishswish();

return 0;
}
 

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
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top