将 parent 静态转换为 child C++
statically casting parent to child C++
我有一个 parent class 和一个 child class,它具有 parent class 的特定功能没有。我创建了一个 Parent 类型的向量,并向其添加了一个 Child 类型的 object。我确定该项目是 child,因此我尝试使用 static_cast 创建指向它的指针,以便使用其 child 特定功能,但它拒绝转换给出下面代码中描述的错误。
#include <vector>
//Parent Class
class Parent {
public:
Parent() {};
};
//Child Class
class Child : public Parent
{
public:
void childSpecificFunction() {};
};
int main()
{
//I know for a fact that the item is a child
std::vector<Parent> items;
items.push_back(Child());
//Static cast to use child specific function doesn't work
Child* childPtr = static_cast<Child*>(items[0]);
childPtr->childSpecificFunction();
//Gives intellisence error: no suitable conversion from "Parent" to "Child *" exists
}
为什么会这样?有没有更好的方法来处理这种事情?
谢谢。
Why is this happening
我们来剖析一下:
int main()
{
//I know for a fact that the item is a child
std::vector<Parent> items;
items.push_back(Child()); // <<<< You are copying and slicing here
//Static cast to use child specific function doesn't work
Child* childPtr = static_cast<Child*>(items[0]);
childPtr->childSpecificFunction(); // <<<<< undefined behavior
//Gives intellisence error: no suitable conversion from "Parent" to "Child *" exists
}
一个合适的修复
int main()
{
std::vector<std::unique_ptr<Parent>> items;
items.push_back(std::make_unique<Child>()); // <<< working with pointers,
// no slicing.
Child* childPtr = static_cast<Child*>(items[0].get());
childPtr->childSpecificFunction();
}
实际上,您正在尝试存储对象,然后将其转换为指向子对象的指针*
#include <vector>
//Parent Class
class Parent {
public:
Parent() {};
};
//Child Class
class Child : public Parent
{
public:
void childSpecificFunction() {};
};
int main()
{
std::vector<Parent*> items;
items.push_back(new Child());
Child* childPtr = static_cast<Child*>(items[0]);
childPtr->childSpecificFunction();
}
另一种方法是转换子对象的地址
#include <vector>
//Parent Class
class Parent {
public:
Parent() {};
};
//Child Class
class Child : public Parent
{
public:
void childSpecificFunction() {};
};
int main()
{
std::vector<Parent> items;
items.push_back(Child());
Child* childPtr = static_cast<Child*>(&(items[0]));
childPtr->childSpecificFunction();
}
我有一个 parent class 和一个 child class,它具有 parent class 的特定功能没有。我创建了一个 Parent 类型的向量,并向其添加了一个 Child 类型的 object。我确定该项目是 child,因此我尝试使用 static_cast 创建指向它的指针,以便使用其 child 特定功能,但它拒绝转换给出下面代码中描述的错误。
#include <vector>
//Parent Class
class Parent {
public:
Parent() {};
};
//Child Class
class Child : public Parent
{
public:
void childSpecificFunction() {};
};
int main()
{
//I know for a fact that the item is a child
std::vector<Parent> items;
items.push_back(Child());
//Static cast to use child specific function doesn't work
Child* childPtr = static_cast<Child*>(items[0]);
childPtr->childSpecificFunction();
//Gives intellisence error: no suitable conversion from "Parent" to "Child *" exists
}
为什么会这样?有没有更好的方法来处理这种事情?
谢谢。
Why is this happening
我们来剖析一下:
int main()
{
//I know for a fact that the item is a child
std::vector<Parent> items;
items.push_back(Child()); // <<<< You are copying and slicing here
//Static cast to use child specific function doesn't work
Child* childPtr = static_cast<Child*>(items[0]);
childPtr->childSpecificFunction(); // <<<<< undefined behavior
//Gives intellisence error: no suitable conversion from "Parent" to "Child *" exists
}
一个合适的修复
int main()
{
std::vector<std::unique_ptr<Parent>> items;
items.push_back(std::make_unique<Child>()); // <<< working with pointers,
// no slicing.
Child* childPtr = static_cast<Child*>(items[0].get());
childPtr->childSpecificFunction();
}
实际上,您正在尝试存储对象,然后将其转换为指向子对象的指针*
#include <vector>
//Parent Class
class Parent {
public:
Parent() {};
};
//Child Class
class Child : public Parent
{
public:
void childSpecificFunction() {};
};
int main()
{
std::vector<Parent*> items;
items.push_back(new Child());
Child* childPtr = static_cast<Child*>(items[0]);
childPtr->childSpecificFunction();
}
另一种方法是转换子对象的地址
#include <vector>
//Parent Class
class Parent {
public:
Parent() {};
};
//Child Class
class Child : public Parent
{
public:
void childSpecificFunction() {};
};
int main()
{
std::vector<Parent> items;
items.push_back(Child());
Child* childPtr = static_cast<Child*>(&(items[0]));
childPtr->childSpecificFunction();
}