wiki:QuestionsAndAnswers

QuestionsAndAnswers

If you have a question, not answered here, please add it with a new empty section with your question used as section title.

Here is an example:

== What is your question? ==

Why does cppcheck complain about my assignment operators?

There is a message assignment operator operatorEq: foo::operator=' should return 'foo &. It indicates that your assignment operator may have unexpected behaviour. Three patterns can be found frequently, differing by the return type to be void, a const reference or a non-const reference. Depending on this signature the properties are quite different, as the example code shows:

class Foo_void {
public:
        void operator = (const Foo_void &rhs)
        {  /*do some assignment of members here*/
                return; }
        // member declarations here
};

class Foo_constref {
public:
        const Foo_constref & operator = (const Foo_constref &rhs)
        {  /*do some assignment of members here */
                return *this; }
        // member declarations here
};

class Foo_ref
{
public:
        Foo_ref & operator = (const Foo_ref &rhs)
        {  /*do some assignment of members here */
                return *this; }
        // member declarations here
};

void test()
{
        Foo_void foo_void1, foo_void2, foo_void3;
        (foo_void1=foo_void2)=foo_void3; // <-- does not compile
        Foo_constref foo_constref1, foo_constref2, foo_constref3;
        (foo_constref1=foo_constref2)=foo_constref3; // <-- does not compile
        Foo_ref foo_ref1, foo_ref2, foo_ref3;
        (foo_ref1=foo_ref2)=foo_ref3; // <-- that does compile
}

The last implementation (non-const reference) is the most versatile and there recommend by Scott Meyers in his Effective C++ books.

What keywords are defined for tickets in the tracker?

For usage and some prominent examples read KeywordsForTickets. If you are searching for the meaning of a specific keyword, ask by adding it to the definition list.

Last modified 10 years ago Last modified on 02/04/2016 07:37:53 PM
Note: See TracWiki for help on using the wiki.