我不能分配从指针到指针到派生类和指针指向基类? [英] I can't assign from pointer to pointer to derived class and pointer to pointer to base class?

查看:111
本文介绍了我不能分配从指针到指针到派生类和指针指向基类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

派生**到基础之间的转换**

在经过几年的Python之后,我又回到了C ++,并且打击了一个强烈的墙。我想我有一个良好的基本多态性和类型转换基指针和派生类之间的处理(例如),但是,这里是一个stumper:为什么我不能为一个派生类的指针指定一个p到p到它的基类?

I'm getting back into C++ after several years of mostly Python, and am hitting up against a strongly-typed wall. I think I have a good handle on basic polymorphism and type-casting between pointers of base and derived classes (e.g. Can a pointer of a derived class be type cast to the pointer of its base class?), but here's a stumper: why can't I assign a pointer to a pointer to a derived class to a p-to-p to it's base class?

对于更多的上下文和可能的提示,这样做更少pythonishly),这里是一个缩小版本的我想要做的。我想有一个指向对象(从一个单一类派生)和一个标识它们的字符串(即 map< string,obj *> )的指针列表。然后,一组组件将传递识别字符串和位置的列表,以存储指向相应对象的指针(即 map )。然后我应该能够通过其字符串id找到合适的对象,并填充适当的指针,供组件随后使用。
这样做的简化代码是

For more context (and perhaps tips on doing this less pythonishly), here's a reduced version of what I'm trying to do. I want to have a list of pointers to objects (derived from a single class) and a string identifying them (i.e. a map<string, obj*>). A set of components will then pass a list of identifying strings and locations to store a pointer to the corresponding object (i.e. a map<string, obj**>). I should then be able to find the appropriate object by its string id, and fill in the appropriate pointer for subsequent use by the component. Simplified code to do this is

#include <map>
#include <string>
using namespace std;

class base
{
};

class derived: public base
{

};

typedef map<string, base**> BasePtrDict;
typedef map<string, base*> BaseDict;


int main(int argc, char* argv[])
{
    base b1, b2;
    derived d;
    derived* d_ptr;

    BaseDict base_dict;
    base_dict["b1"] = &b1;
    base_dict.insert(make_pair("b2",&b2)); // alternate syntax
    base_dict["d"]= &d;

    BasePtrDict ptr_dict;
    ptr_dict["d"] = &d_ptr;

    for (auto b = ptr_dict.begin(); b != ptr_dict.end(); b++)
        *(b->second) = base_dict[b->first];

    return 0;
}

这会遇到编译错误 ptr_dict [ d] =& d_ptr; 。为什么?在C ++范例中,我应该怎么办?我真的需要做到丑陋(不安全吗?) reinterpret_cast< base>()到处吗?

This runs into a compile error at ptr_dict["d"] = &d_ptr;. Why? In the C++ paradigm, what should I be doing? Do I really need to do ugly (unsafe?) reinterpret_cast<base>() everywhere?

推荐答案

您失去了必要的信息,才能将 base * 转换为派生*

You're losing the necessary information to be able to cast a base * to a derived *.

考虑派生从多个基类继承的情况,因此cast需要调整指针值。然后, derived * pd = static_cast< derived *>(pb)用于指向基数 pb 应用指针调整。

Consider the case where derived inherits from multiple base classes, so a cast needs to adjust the pointer value. Then derived *pd = static_cast<derived *>(pb) for a pointer-to-base pb will automatically apply the pointer adjustment.

derived * pd; base ** ppb =& pd; * ppb = * pb 将无法应用指针调整,因此这是不合法的。

However derived *pd; base **ppb = &pd; *ppb = *pb will fail to apply the pointer adjustment, so this cannot be legal.

你应该做的是: p>

What you should be doing is:

base *b_ptr;
... // existing code
derived *d_ptr = static_cast<derived *>(b_ptr);

这篇关于我不能分配从指针到指针到派生类和指针指向基类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆