V603. Object was created but not used. If you wish to call constructor, use 'this->Foo::Foo(....)'.
The analyzer has detected a potential error related to the incorrect use of a constructor. Programmers may make mistakes when trying to explicitly call a constructor to initialize an object.
Take a look at a typical sample taken from a real application:
class CSlideBarGroup
{
public:
CSlideBarGroup(CString strName, INT iIconIndex,
CListBoxST* pListBox);
CSlideBarGroup(CSlideBarGroup& Group);
...
};
CSlideBarGroup::CSlideBarGroup(CSlideBarGroup& Group)
{
CSlideBarGroup(Group.GetName(), Group.GetIconIndex(),
Group.GetListBox());
}
There are two constructors in the class. To shorten the code, a programmer decided to call one constructor from the other. However, the code behavior differs from what the developer expected.
A new unnamed object of the CSlideBarGroup
type is created and gets destroyed right after. As a result, the class fields remain uninitialized.
To fix it, create an initialization function and call it from the constructors. This is the correct code:
class CSlideBarGroup
{
void Init(CString strName, INT iIconIndex,
CListBoxST* pListBox);
public:
CSlideBarGroup(CString strName, INT iIconIndex,
CListBoxST* pListBox)
{
Init(strName, iIconIndex, pListBox);
}
CSlideBarGroup(CSlideBarGroup& Group)
{
Init(Group.GetName(), Group.GetIconIndex(),
Group.GetListBox());
}
...
};
If calling the constructor is necessary, the following can be written:
CSlideBarGroup::CSlideBarGroup(CSlideBarGroup& Group)
{
this->CSlideBarGroup::CSlideBarGroup(
Group.GetName(), Group.GetIconIndex(), Group.GetListBox());
}
Another identical option:
CSlideBarGroup::CSlideBarGroup(CSlideBarGroup& Group)
{
new (this) CSlideBarGroup(
Group.GetName(), Group.GetIconIndex(),
Group.GetListBox());
}
The code in the examples is dangerous, so it's important to have a good understanding of how it works!
This code can do more harm than good.
Let's look at an example where such a constructor call is allowed—and one where it isn't.
class SomeClass
{
int x,y;
public:
SomeClass() { SomeClass(0,0); }
SomeClass(int xx, int yy) : x(xx), y(yy) {}
};
The code contains an error. In the SomeClass()
constructor, a temporary object is created. As a result, the x
and y
fields remain uninitialized. You can fix the code in this way:
class SomeClass
{
int x,y;
public:
SomeClass() { new (this) SomeClass(0,0); }
SomeClass(int xx, int yy) : x(xx), y(yy) {}
};
This code is safe and works well because the class contains primary data types and is not a descendant of other classes. In this case, the double constructor call is harmless.
Consider another code where the explicit constructor call causes an error:
class Base
{
public:
char *ptr;
std::vector vect;
Base() { ptr = new char[1000]; }
~Base() { delete [] ptr; }
};
class Derived : Base
{
Derived(Foo foo) { }
Derived(Bar bar) {
new (this) Derived(bar.foo);
}
}
When a programmer calls the new (this) Derived(bar.foo);
constructor, the Base
object is already created, and the fields are initialized. Calling the constructor again leads to double initialization. A pointer to the newly allocated memory space is written to ptr
. The result will be a memory leak. The outcome of double-initializing an std::vector
type object is difficult to predict, making this code unacceptable.
Instead of explicitly calling the constructor, it is recommended to create an initialization function. An explicit constructor call is required only in exceptional cases.
Explicit call of one constructor from the other in C++11 (delegation)
The new standard allows calling class constructors from other ones (delegation). This enables developers to create constructors that use the behavior of other constructors without duplicating code.
This is an example of correct code:
class MyClass {
int m_x;
public:
MyClass(int X) : m_x(X) {}
MyClass() : MyClass(33) {}
};
The MyClass
constructor without arguments calls a constructor of the same class with an integer argument.
C++03 considers an object to be constructed when its constructor finishes executing, but C++11 considers an object constructed once any constructor finishes execution. Derived class constructors will execute after all delegation in their base classes is complete.
Additional information
- Discussion on Stack Overflow. C++'s "placement new".
- Discussion on Stack Overflow. Using new (this) to reuse constructors.
This diagnostic is classified as:
|
You can look at examples of errors detected by the V603 diagnostic. |