Inefficient usage

Code can be made more efficient by taking advantage of assignment operators.

The C++ standard template library defines a number of standard classes. Some operators tend to create and destroy temporary objects. This warning message is used to point out an alternative way to compute the same result more efficiently, by reducing the number of temporaries required.

ID

Observation

Description

1

Call site

The place where the operator was invoked

2

Definition

The place where the operator was defined

Example

          
#include <string>
#include <iostream>

using namespace std;

class myClass
{
    public:
    int i;
    int j;

    myClass(int i, int j)
    {
        this->i = i;
        this->j = j;
    }

    myClass & operator +=(const myClass &b)
    {
        this->i = this->i + b.i;
        return *this;
    }

    myClass operator   +(const myClass &b)
    {
        *this += b;
        return *this;
    }
};

int main(int argc, char **argv)
{
    A a(1, 1), b(1, 1);
    a = a + b;     // better is a += b;
    cout << a.i;
    return 0;
}
        

Copyright © 2010, Intel Corporation. All rights reserved.