铸造指针

发布于 2024-09-06 22:35:32 字数 830 浏览 12 评论 0原文

我使用 ptr_map 来存储不同类型的指针。

boost::ptr_map<string, any> someMap;

我在那里存储一些模板化的类对象:

someMap.insert("1", new SomeClass<int>());
someMap.insert("2", new SomeClass<float>());

现在我想从地图获取值。这是一个带有引用的示例:

template<typename T>
T &get(const string &someKey)
{
    try
    {
        return any_cast<EventType&>(mSignalAssociation.at(signalName));
    } catch(bad_any_cast &e)
    {
        // Logging here
    }
}

get< SomeClass<int> >("1"); // This works

但我不喜欢引用,因为我无法返回,例如,如果转换错误或对象不存在,则无法返回 NULL。

如何从该地图获取指针?

T *get(const string &someKey)
{
   return any_cast<EventType*>(mSignalAssociation.at(signalName));
}

可以构建,但铸造失败,为什么?

I'm using ptr_map for storing different types of pointers.

boost::ptr_map<string, any> someMap;

I store there some templated class objects:

someMap.insert("1", new SomeClass<int>());
someMap.insert("2", new SomeClass<float>());

Now I want to get values from map. Here is a sample with references:

template<typename T>
T &get(const string &someKey)
{
    try
    {
        return any_cast<EventType&>(mSignalAssociation.at(signalName));
    } catch(bad_any_cast &e)
    {
        // Logging here
    }
}

get< SomeClass<int> >("1"); // This works

But I don't like references, because I can't return, for example, NULL if the casting is bad or the object doesn't exist.

How can I get pointer from this map?

T *get(const string &someKey)
{
   return any_cast<EventType*>(mSignalAssociation.at(signalName));
}

This builds but fails at casting, why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

混浊又暗下来 2024-09-13 22:35:32

这个怎么样:(

T *get(const string &someKey)
{
   return &any_cast<EventType&>(mSignalAssociation.at(signalName));
}

只是猜测)

how about this:

T *get(const string &someKey)
{
   return &any_cast<EventType&>(mSignalAssociation.at(signalName));
}

(Just a guess)

尴尬癌患者 2024-09-13 22:35:32

函数any_cast

如果传递一个指针,
它返回一个类似的合格的
指向值内容的指针 if
成功,否则为 null
回来了。如果 T 是 ValueType,则
返回持有值的副本,
否则,如果 T 是对
(可能是 const 限定的)ValueType,
它返回对所持有的引用
值。

您想要的是指针语义。还要停止使用 ptr_map,正如评论中指出的那样,这是一种浪费。

map<string, any> someMap;
someMap["1"] = SomeClass<int>();
someMap["2"] = SomeClass<float>();

// this will be a valid pointer because someMap["1"] stores 
// an object of SomeClass<int>
SomeClass<int>* valid_ptr = any_cast<SomeClass<int> >(&someMap["1"]);

// this will be null pointer because someMap["2"] doesn't store
// an object of SomeClass<int>
SomeClass<int>* invalid_ptr = any_cast<SomeClass<int> >(&someMap["2"]);

如果您出于某种原因需要存储指向这些 SomeClass 对象的指针,那么我认为您必须自己进行内存管理(手动释放存储在 any 中的元素)并使用额外的间接级别来检测 null 的强制转换失败指针。如果你这样做的话,最好使用像 boost::shared_ptr 这样的东西。

map<string, any> someMap;
someMap["1"] = new SomeClass<int>();
someMap["2"] = new SomeClass<float>();

// this will be a valid pointer because someMap["1"] stores 
// an object of SomeClass<int>*
SomeClass<int>** valid_ptr = any_cast<SomeClass<int>*>(&someMap["1"]);

// this will be a null pointer because someMap["1"] does
// not store an object of SomeClass<int>*
SomeClass<int>** invalid_ptr = any_cast<SomeClass<int>*>(&someMap["2"]);

Function any_cast

If passed a pointer,
it returns a similarly qualified
pointer to the value content if
successful, otherwise null is
returned. If T is ValueType, it
returns a copy of the held value,
otherwise, if T is a reference to
(possibly const qualified) ValueType,
it returns a reference to the held
value.

What you want are the pointer semantics. Also stop using ptr_map, it's a waste as has been pointed out in the comments.

map<string, any> someMap;
someMap["1"] = SomeClass<int>();
someMap["2"] = SomeClass<float>();

// this will be a valid pointer because someMap["1"] stores 
// an object of SomeClass<int>
SomeClass<int>* valid_ptr = any_cast<SomeClass<int> >(&someMap["1"]);

// this will be null pointer because someMap["2"] doesn't store
// an object of SomeClass<int>
SomeClass<int>* invalid_ptr = any_cast<SomeClass<int> >(&someMap["2"]);

If you need to store pointers for some reason to these SomeClass objects, then I think you'll have to do the memory management yourself (manually free the elements stored in any) and use an extra level of indirection to detect cast failures with a null pointer. Probably better to use something like boost::shared_ptr if you do.

map<string, any> someMap;
someMap["1"] = new SomeClass<int>();
someMap["2"] = new SomeClass<float>();

// this will be a valid pointer because someMap["1"] stores 
// an object of SomeClass<int>*
SomeClass<int>** valid_ptr = any_cast<SomeClass<int>*>(&someMap["1"]);

// this will be a null pointer because someMap["1"] does
// not store an object of SomeClass<int>*
SomeClass<int>** invalid_ptr = any_cast<SomeClass<int>*>(&someMap["2"]);
孤凫 2024-09-13 22:35:32

但我不喜欢引用,因为我
不能返回,例如,如果
铸造不好或物体没有
存在

啊...这就是为什么你要使用指针、引用、转换和内存管理容器进行这些练习的原因! :)

正如我之前指出的之前;这会带来不必要的麻烦。
幸运的是,您的问题已经解决 - 只需查看 增强。可选

But I don't like references, because I
can't return, for example, NULL if the
casting is bad or the object doesn't
exist

Ah... so that is the reason why you are doing these gymnastics with pointers, references, casting and memory managing containers! :)

As I pointed out previously; this will create unnecessary headaches.
Fortunately, your problem has been solved - just look at Boost.Optional.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文