如何创建在C ++升压指针运算符重载? [英] How do I create overloaded operators for boost pointers in C++?

查看:131
本文介绍了如何创建在C ++升压指针运算符重载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从这个职位,我看到,你不能超载运营商指针:<一href=\"http://stackoverflow.com/questions/1419997/c-operator-overloading-of-for-pointers-to-objects\">http://stackoverflow.com/questions/1419997/c-operator-overloading-of-for-pointers-to-objects

From this post I see that you can't overload operators for pointers: http://stackoverflow.com/questions/1419997/c-operator-overloading-of-for-pointers-to-objects

但有什么办法,我可以重载运营商提升指针?例如:

But is there any way I could overload operators for boost pointers? For example:

  boost::shared_ptr<ClassB> operator||(boost::shared_ptr<ClassA> lhs, boost::shared_ptr<ClassA> rhs) {
    boost::shared_ptr<ClassB> returnVal = CreateClassBSharedPtr(lhs, rhs);
    return returnVal;
  }

在尝试,我得到一个模棱两可的过载错误(它违反了内置的操作符||(布尔,布尔))。任何想法来解决这个问题?

When attempting this, I get an ambiguous overload error (it conflicted with the built in operator||(bool, bool)). Any ideas to get around this?

编辑:添加一些更多的细节如下,为什么我想做到这一点

我会尽力解释我试图尽我所能。我想要做的是做一个验证器对象,可以检查是否某些属性持有的地图。例如:

I'll try to explain what I'm attempting as best I can. What I'd like to do is make a "validator" object for maps that can check if certain properties hold. For example:

boost::shared_ptr<MyValidator> my_validator = IsEmpty("key name 1") && IsNotEmpty("key name 2") || HasNElements("key name 3", num)

后来,为了验证图:

Later, to validate a map:

if(my_validator.validate(some_map)) { ... }

我想我坚持使用指针,因为我不能用值传递(因为我利用多态性),我不能用const引用使用通(因为会有被创建的临时对象试图验证时,不会存在后期运营嵌套)。

I think I'm stuck with using pointers because I can't use pass by value (since I'm making use of polymorphism) and I can't use pass by const reference (since there would be temporary object created by nesting operators that would not exist later when trying to validate).

修改:增加了专门针对我的问题,新的问题在这里: http://stackoverflow.com/questions/3441608/implementation-suggestions-for-creating-an-ex$p$pssion-later-used-to-evaluate-the

推荐答案

当然,您可以:

#include "boost/shared_ptr.hpp"

template <class A>
bool operator||(boost::shared_ptr<A> lhs, boost::shared_ptr<A> rhs) {
    return true;
}

int main() {
    boost::shared_ptr <int> a, b;
    bool x = a || b;
}

您是否可以做你在你的例子在做什么,我不知道,因为我不知道你正在尝试做的!

Whether you can do what you are doing in your example, I don't know, as I'm not sure what you are trying to do!

这篇关于如何创建在C ++升压指针运算符重载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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