List:MySQL++« Previous MessageNext Message »
From:Chris Frey Date:July 22 2005 2:45am
Subject:Re: Beta 2: Warning when compiling program in FC4 GCC4
View as plain text  
On Thu, Jul 21, 2005 at 06:29:03PM -0600, Warren Young wrote:
> Chris Frey wrote:
> >
> >The latest SVN version has this "fixed", but incorrectly in my
> >opinion.  It currently just returns *this, whereas I don't think
> >operator=() should be defined at all, just declared.
> 
> Does this actually suppress the compiler-generated version?

Yep.

tmp $ cat oe.cc
class A
{
private:
        A(const A&);
        A& operator=(const A&);

        int i;

public:
        A() : i(5) {}
};

int main()
{
        A a, b;
        a = b;
        return 0;
}

tmp $ g++ -Wall -o oe oe.cc
oe.cc: In function `int main()':
oe.cc:5: error: `A& A::operator=(const A&)' is private
oe.cc:16: error: within this context


This also flags an error for derived classes too.  The thing you want
to be careful about is if you're doing virtual operator=(), with a
base class as the const reference argument.  For example:

class A {
public:
	virtual A& operator=(const A&);	// this overrides the default
};

class B : public A {
public:
	virtual A& operator=(const A&);	// this does not
	// there is a hidden, automatic, operator=(const B&) here too!
	// and the automatic operator=() will call the user-defined
	// A::operator=() for us.
};



> Regardless, operator= does normally return *this, so the only problem 
> with doing it this way is that it generates a tiny bit more code than if 
> it were only declared and not defined.

I think all we care about is the compiler warning, so no copy is done
accidentally.

- Chris

Thread
m4 macro for mysql++?Pål Brattberg13 Jul
  • Re: m4 macro for mysql++?Warren Young13 Jul
    • Beta 2: Warning when compiling program in FC4 GCC4Carlos M. Gutierrez21 Jul
      • Re: Beta 2: Warning when compiling program in FC4 GCC4Carlos M. Gutierrez21 Jul
      • Re: Beta 2: Warning when compiling program in FC4 GCC4Chris Frey22 Jul
        • Re: Beta 2: Warning when compiling program in FC4 GCC4Warren Young22 Jul
          • Re: Beta 2: Warning when compiling program in FC4 GCC4Chris Frey22 Jul