?
Solved

operator=(const &myclass)

Posted on 2002-06-21
6
Medium Priority
?
399 Views
Last Modified: 2010-05-18
I've just spent a couple of hours tracking down an error, which turned out to be that a copy assignment operator was mistyped - what should have been

myclass& myclass::operator=(const myclass& m);

was actually written as

myclass& myclass::operator=(const &myclass m);

My question is - why did the second version compile at all?  Does it mean anything?
0
Comment
Question by:IainHere
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 49

Expert Comment

by:DanRollins
ID: 7099598
What compiler?  In VC++,

class CMyClass {
public:
     int n;
     CMyClass& operator=(const &CMyClass m);

};

returns:

 error C2146: syntax error : missing ',' before identifier 'm'
 error C2061: syntax error : identifier 'm'

-- Dan
0
 
LVL 4

Expert Comment

by:mblat
ID: 7099739


const& myclass m would mean "m is a const reference to a myclass". But that is redundant, since references are always const. You can't change a reference. Never.

In other words, "myclass& const m" is the same thingequivalent to "myclass& x". Since you're gaining nothing by adding the const after the &, you shouldn't add.
 So it is actually suprising that your compiler didn't complain, because this constact is nonsence.
0
 
LVL 4

Author Comment

by:IainHere
ID: 7103125
Dan - my apologies.  It was actually
CMyClass& operator=(const &CMyClass)
the operator was declared as private to stop anything using the default one, so the parameter wasn't actually being used so it wasn't named.

mblat - "references are always const".  I think I must be misunderstanding what you're saying - references don't have to to be const - if you pass them non-const, you can alter them as if they were pointers, but with object semantics.  Am I missing something?  Anyway, I'm sure the two things you state are equivalent are not.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 9

Accepted Solution

by:
jasonclarke earned 200 total points
ID: 7103179
> CMyClass& operator=(const &CMyClass)

this is actually a real nasty, what this actually means is:
 
  CMyClass& operator=(const int& CMyClass)

i.e. declare an operator that takes a variable of type int (implicit) with name CMyClass.

try the following code to show it better:

class X
{
    operator=(const &X)
    {
         int i = X;
    }
};

this compiles just fine with VC++ -

it appears to be a VC++ failure, if you try to compile the same code with gcc, you get the following messages:

test.cpp:4: ISO C++ forbids declaration of `operator=' with no type
test.cpp:4: `X::operator= (...)' must take exactly one argument
test.cpp:4: `X::operator= (...)' must take exactly one argument
0
 
LVL 4

Author Comment

by:IainHere
ID: 7103657
Excellent!  Thanks for the description.

[apologies for leading you the merry dance, Dan - I'm sure you'll forgive me]
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7104877
>>Dan - my apologies.  It was actually CMyClass& operator=(const &CMyClass)

Well that is irritating beyond words.  But I accept your appology :-)

-- Dan
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

766 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question