I am learning static_cast<Type>(object)
in C++. I found it calls copy constructor of Type
with object
. Is is true? If it is true, why it copies it? I thought it just change how to use the memory pointed by object. If it needs constructing copy, static_cast
has more cost than I thought. Do I need to care of the cost?
The followings are the test code,
#include <iostream>
class Base {
public:
Base() {};
Base(const Base& org) {
std::cout << "Base Copy Constructor" << std::endl;
};
virtual ~Base() {};
};
class Derived : public Base {
public:
void static_casting(void) {
static_cast<Base>(*this);
}
};
void test_static_cast_copy_constructor(void) {
Derived a;
a.static_casting();
}
Thank you very much.