English 中文(简体)
我如何利用智能点点瞄准兄弟的物体?
原标题:How do I use smart pointer to point at sibling class objects?

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

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.

I think,
Parent ptr = new Child1() a non smart pointers way, should work in this case if I do (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?

unique_ptr<Parent> ptr = make_unique<Child1>();?

问题回答

Parent ptr = new Child1() will not compile, since ptr is not declared as a pointer. You need to add a * to it, eg:

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:

Parent *ptr = new Child1();
...
delete ptr;
ptr = new Child2();
...
delete ptr;

Don t forget to add a virtual destructor to Parent!

现在,你可以与智能点人做同样的事,例如:

unique_ptr<Parent> ptr = make_unique<Child1>();
...
ptr = make_unique<Child2>();

我个人认为,使用智能指使器通常更能使用正常点,因为通常点人,你必须放弃记忆。

It is important that you choose the smart pointer that needs to do the right things according to what you expect from your code:

  • Unique pointers basically consider itself the only owner of the resource, matter of fact it does not support copying as you can see from unique ptr operator=;
  • Shared Pointers allow you to share the responsability throughout different pointers (every time you copy the pointer you increase the reference counter associated to the resource, callable with the method use_count())

你们可以以不同的方式分配一个智能点:

  • 在你的案件中,我们有:

     std::unique_ptr <BASE_CLASS_TYPE> ptr = make_unique <SUBCLASS_TYPE>(CONSTRUCTOR_ARGS);
    

• 使我们能更高效、更安全地建立点(谈论排外安全);

  • a. 如果没有公摊或公摊;

     std::unique_ptr <SUPERCLASS_TYPE> ptr (new SUBCLASS_TYPE());
    

为了超出标准,你可以采用“滚动”方法:reset(>):如果存在旧资源,请删除者,重新分配新资源。

    ptr.reset(new OTHER_SUBCLASS_TYPE());

For your case the type passed in the reset function needs to be a concrete class of the super class which pointer has been declared with.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签