Recently, in a class in a project, if you encounter a virtual virtual function that wants to cast a subclass into a parent class, then call its parent version.
it will appear . GCC compilation error Hint: Error:ld returned 1 exit status
GCC hint reason: In the link process, found unable to find the reference symbol, the reference symbol is the copy constructor of this parent class.
From this, I infer that the reason for the problem is that the copy constructor of the parent class is called by default in the process of converting a subclass into a parent class. Disallow_copy_and_assign (XXX) is used for most classes in the project; This macro declaration is not implemented, which is equivalent to banning the copy constructor. The macro is defined as follows:
#define DISALLOW_COPY_AND_ASSIGN (ClassName) ClassName (const classname&); void operator= (const classname&)
The workaround is to convert its type, but not to use the copy constructor (copy constructor), as follows:
With parrent& instead of static_cast<parrent> (*this), using references instead of classes, you can avoid copying new objects and naturally avoid calling their copy constructors.
Reference:
1. Http://stackoverflow.com/questions/9084835/why-does-static-castthis-to-a-base-class-create-a-temporary-copy
Problems with the child class of C + + and the parent class cast