I am a little confused with new smart pointers. I want to use one pointer to point at two different objects of sibling classes (same parent).
我对新的智能指针感到有点困惑。我想使用一个指针来指向兄弟类的两个不同对象(相同的父类)。
so basically I have a parent class Parent
and two child classes Child1
and Child2
所以基本上我有一个父类Parent和两个子类Child1和Child2
class Parent{
virtual void foo() = 0;
};
class Child1 : public Parent{
void foo(){
//something
}
}
class Child2 : public Parent{
void foo(){
//something different
}
}
The pointer say ptr
is first pointing at Child1
and then it should point at Child2
Doing so I want to use the same pointer to behave differently as per the implementations inside those sibling classes.
指针说Ptr首先指向Child1,然后它应该指向Child2,这样做,我希望使用相同的指针来表现与那些兄弟类中的实现不同的行为。
I think,
Parent ptr = new Child1()
a non smart pointers way, should work in this case if I do (Child2 *)ptr
我认为,父Ptr=new Child1()是一种非智能指针方式,如果我这样做(Child2*)Ptr,在这种情况下应该可以工作
But I am sure there would be a better way through smart pointers.
但我相信,通过聪明的指针会有更好的方法。
I know that reinterpret_ptr does have some usecase around this topic but i couldn't understand the explanation on gfg. ALSO my main Question is How do I declare this pointer?
我知道reinterpret_ptr在这个主题上确实有一些用处,但我不明白gfg上的解释。另外,我的主要问题是如何声明这个指针?
unique_ptr<Parent> ptr = make_unique<Child1>();
?
UNIQUE_PTR
PTR=Make_Unique
();?
更多回答
I think, Parent ptr = new Child1() a non smart pointers way, should work in this case if I do (Child2 *)ptr
No it won't. ptr
points to a Child1
. There is no Child2
. Don't cast it to one.
我认为,父Ptr=new Child1()是一种非智能指针方式,如果我这样做(Child2*)Ptr No它不会起作用。Ptr指向Child1。没有孩子2。别把它扔给一个人。
Your "non smart pointer way" is undefined behavior.
你的“非智能指针方式”是未定义的行为。
You cannot have a pointer to Child1
and pretend it's a pointer to Child2
. Whatever casts you use, it's still wrong.
您不能将指向Child1的指针设为指向Child2的指针。不管你用什么石膏,它仍然是错误的。
"The pointer say ptr
is first pointing at Child1
and then it should point at Child2
" -- this is not phrased correctly for how pointers work. Pointers point to objects, not classes. So you could say that ptr
first points to an object of type Child1
, and then it should point to an object of type Child2
(which is necessarily a different object with just what's in the question).
“指针说PTR首先指向Child1,然后它应该指向Child2”--对于指针的工作方式来说,这是不正确的措辞。指针指向对象,而不是类。因此,您可以说,ptr首先指向一个Child1类型的对象,然后它应该指向一个Child2类型的对象(这必然是一个与问题中的内容不同的对象)。
Casts are not a magic way to convert one object type into another. How would the compiler know how to do that? Casts are basically just a way of saying to the compiler "shut up, I know what I'm doing and if you interpret this foo as a bar it'll be fine", but it's on you to then make sure that the cast actually makes sense and if it doesn't, then you have UB on your hands. Casts are rarely the correct solution - sure, cases exist where casts make sense - they are few and far between and you really should be a C++ expert before using them.
强制转换不是将一种对象类型转换为另一种对象类型的神奇方法。编译器如何知道如何做到这一点?强制转换基本上只是一种对编译器说“闭嘴,我知道我在做什么,如果你把这个foo解释为一个bar就好了”的一种方式,但接下来要由你来确保强制转换真正有意义,如果它不有意义,那么你就有了UB。强制转换很少是正确的解决方案-当然,在某些情况下强制转换是有意义的-它们很少,在使用它们之前,您确实应该成为一名C++专家。
优秀答案推荐
Parent ptr = new Child1()
will not compile, since ptr
is not declared as a pointer. You need to add a *
to it, eg:
父ptr=new Child1()将不会编译,因为ptr没有声明为指针。你需要在它后面加上一个*,例如:
Parent *ptr = new Child1();
Now then, (Child2 *)ptr
is undefined behavior, because a Child1
object is not a valid Child2
object and vice versa, so you can't just type-cast the pointer. You need to point at a whole separate Child2
object, eg:
现在,(Child2*)PTR是未定义的行为,因为Child1对象不是有效的Child2对象,反之亦然,所以您不能只对指针进行类型转换。你需要指向一个完全独立的Child2对象,例如:
Parent *ptr = new Child1();
...
delete ptr;
ptr = new Child2();
...
delete ptr;
Don't forget to add a virtual
destructor to Parent
!
别忘了给Parent添加一个virtual析构函数!
Now, you can do the same thing with smart pointers, eg:
现在,你可以用智能指针做同样的事情,例如:
unique_ptr<Parent> ptr = make_unique<Child1>();
...
ptr = make_unique<Child2>();
更多回答
我是一名优秀的程序员,十分优秀!