Click here to Skip to main content
15,887,596 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionA non-const reference may only be bound to an lvalue? Pin
George_George13-Dec-07 18:50
George_George13-Dec-07 18:50 
AnswerRe: A non-const reference may only be bound to an lvalue? Pin
Iain Clarke, Warrior Programmer14-Dec-07 1:21
Iain Clarke, Warrior Programmer14-Dec-07 1:21 
GeneralRe: A non-const reference may only be bound to an lvalue? Pin
toxcct14-Dec-07 2:38
toxcct14-Dec-07 2:38 
GeneralRe: A non-const reference may only be bound to an lvalue? Pin
Iain Clarke, Warrior Programmer14-Dec-07 3:29
Iain Clarke, Warrior Programmer14-Dec-07 3:29 
GeneralRe: A non-const reference may only be bound to an lvalue? Pin
toxcct14-Dec-07 3:30
toxcct14-Dec-07 3:30 
GeneralRe: A non-const reference may only be bound to an lvalue? Pin
Nelek16-Dec-07 21:11
protectorNelek16-Dec-07 21:11 
GeneralRe: A non-const reference may only be bound to an lvalue? Pin
George_George14-Dec-07 17:56
George_George14-Dec-07 17:56 
GeneralRe: A non-const reference may only be bound to an lvalue? Pin
Iain Clarke, Warrior Programmer14-Dec-07 22:28
Iain Clarke, Warrior Programmer14-Dec-07 22:28 
I know what they're on about now, having read the page you point to.

In short, for those (like me) who don;t want to slog through too much work to help someone...

class A
{
   ...
};

class B
{
public:
    // I think you'd get complained at if both of these were used
    B ( A &a );   // would pop up the error if it was enabled.
    B ( const A &a); // would not pop up the error
    ....
};


...is the example MS use.

As it a warning, and one you have to explicitly enable. So microsoft are labelling using a non-const reference as inadvisable, not a failure.

This non-const reference rule applies to more than constructors. Imagine operator= members, which can be mutated into copy constructors.

void MyFunction (CBigClass &big)
{
    COtherClass other;

    other = big;

    other.DoStuff ();
}


So far so good? big is a big class, involving connecting to databases on construction, etc. You don't want to make new ones, so you pass it by reference or reference. But you don't want the bugs that come by passing by pointer. What if some evil person passed me a NULL? So you pass by reference.

The above function looks pretty innocuous? You would think nothing of using...
CBigClass b;
b.Blah ();

MyFunction (b); // I've looked through this - I can't see anything bad there!
                // This function just takes a copy of big, and then works on the
                // copy. What could possibly go wrong?


Now imagine...

class COtherClass
{
   ....
   COtherClass &operator=(CBigClass &b);
   ....
};

COtherClass &COtherClass::operator=(CBigClass b)
{
   ....
   b.DoSomethingNotConst ();
   ....
   return *this;
}



Suddenly, you have a bug that doesn't manifest until much later, and would be really hard to track down. After all, you're just using =! Where's the harm in that?

And if you say
"But Iain! I'm the mighty George! So mighty, I was called George twice! I would never use a bad member function of a class I'm supposed to just be copying! Nevermind my addiction to exclamation points!!!", then I would say...

"In that case, what's the harm in declaring the = function as
COtherClass &operator=(CBigClass const &b);
after all, you weren't going to anything non-const, were you?"

Most of the time, when things have to be one way, just ask yourself, what if they didn't? You can usually find something bad that would happen.

Here endeth the lesson, from the gospel according to Iain. My fingers are now tired.

Iain.
GeneralRe: A non-const reference may only be bound to an lvalue? Pin
George_George16-Dec-07 2:24
George_George16-Dec-07 2:24 
GeneralRe: A non-const reference may only be bound to an lvalue? Pin
Iain Clarke, Warrior Programmer16-Dec-07 8:02
Iain Clarke, Warrior Programmer16-Dec-07 8:02 
GeneralRe: A non-const reference may only be bound to an lvalue? Pin
George_George16-Dec-07 20:50
George_George16-Dec-07 20:50 
GeneralRe: A non-const reference may only be bound to an lvalue? Pin
Iain Clarke, Warrior Programmer16-Dec-07 22:13
Iain Clarke, Warrior Programmer16-Dec-07 22:13 
GeneralRe: A non-const reference may only be bound to an lvalue? Pin
George_George16-Dec-07 22:35
George_George16-Dec-07 22:35 
QuestionHow to resolve such compile errors in VC++2005 Pin
kcynic13-Dec-07 18:19
kcynic13-Dec-07 18:19 
AnswerRe: How to resolve such compile errors in VC++2005 Pin
Mark Salsbery13-Dec-07 19:27
Mark Salsbery13-Dec-07 19:27 
GeneralRe: How to resolve such compile errors in VC++2005 Pin
kcynic13-Dec-07 19:40
kcynic13-Dec-07 19:40 
GeneralRe: How to resolve such compile errors in VC++2005 Pin
Mark Salsbery13-Dec-07 19:49
Mark Salsbery13-Dec-07 19:49 
GeneralRe: How to resolve such compile errors in VC++2005 Pin
kcynic13-Dec-07 19:57
kcynic13-Dec-07 19:57 
GeneralRe: How to resolve such compile errors in VC++2005 Pin
buidinhba514-Sep-08 17:05
buidinhba514-Sep-08 17:05 
QuestionHow to count number of files in a folder Pin
santhoshv8413-Dec-07 18:17
santhoshv8413-Dec-07 18:17 
AnswerRe: How to count number of files in a folder Pin
kcynic13-Dec-07 18:28
kcynic13-Dec-07 18:28 
GeneralRe: How to count number of files in a folder Pin
santhoshv8413-Dec-07 18:46
santhoshv8413-Dec-07 18:46 
GeneralRe: How to count number of files in a folder Pin
Hamid_RT14-Dec-07 20:04
Hamid_RT14-Dec-07 20:04 
GeneralSeraching criteria Pin
neha.agarwal2713-Dec-07 17:55
neha.agarwal2713-Dec-07 17:55 
GeneralRe: Seraching criteria Pin
Karismatic13-Dec-07 19:09
Karismatic13-Dec-07 19:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.