简体   繁体   English

运算符=与转换运算符结合的歧义

[英]operator= ambiguity in junction with conversion operators

I have the following situation: 我有以下情况:



    class B;

    class A {
    private:
        int n;
    public:
        A& operator=(const A& a) {
        }

        A& operator=(const int n) {
            this->n = n;
        }

        friend class B;
    };

    class B {
    private:
        A a;
    public:
        operator A&() {
            return a;
        }

        operator int&() {
            return a.n;
        }
    };

When I execute this code: 当我执行此代码时:



    A a;
    B b;
    int i = b;
    a = b;
    a = i;

I have the following error: 我有以下错误:



    error C2593: 'operator =' is ambiguous
    ..\CrossPPTest\TestProxy.cpp(40): could be 'A &A::operator =(const int)'
    ..\CrossPPTest\TestProxy.cpp(37): or       'A &A::operator =(const A &)'
    while trying to match the argument list '(A, B)'

How to resolve this ambiguity assuming I can not add 假设我无法添加,如何解决这种歧义

A& operator =(const B&)
to class A. 到A级。

There are complex reasons why I have to do exactly like this, but it would be really nice if thing like this will work. 有很多复杂的原因使我必须完全按照这种方式做,但是如果这种方式行得通,那就太好了。

May be there are some priorities or something like explicit keyword for operators... Any suggestions are highly appreciated. 可能有一些优先级或一些类似运算符的显式关键字...任何建议都将受到高度赞赏。

UPDATE: Any kind of casts can not be used in second part of code. 更新:任何类型的强制转换都不能在代码的第二部分中使用。 The thing is to find a solution modifying the first code part only. 问题是找到仅修改第一个代码部分的解决方案。

ONE MORE UPDATE: Code part #2 MUST compile as is. 一个更新:代码第2部分必须按原样编译。

This looks like a job for polymorphism: 这看起来像是多态的工作:

class B;

class A {
   int n;
   public:
      A& operator=(const A& a) {...}

    A& operator=(const int n) {
      this->n = n;
      return *this;
    }

    friend class B;
};

class B : public A {
   A a;
   public:
     operator int&() {
         return a.n;
     }
};

int main() {
    A a;
    B b;
    int i = b; // works
    a = b; // works
    a = i;
}

Demo 演示版

The way you posed makes the problem substantially unsolvable. 您提出的方式使问题基本上无法解决。

There are clearly two ways to assign to an A from a B , none of which is preferable. 显然,有两种从B分配给A的方法,没有一种是可取的。

The only solution (without touching the classes) is to explicitly cast, so that you force which conversion has to take place. 唯一的解决方案(不涉及类)是显式强制转换,以便您强制进行转换。

In general, assignment and conversion are redundant: if you admit implicit conversion (either to - with U::operator T() const - or from - with T::T(const U&) ) you don't have to provide assignment other than the default, and if you want implicit heterogeneous assignments, you must not provide conversion, or at most make them explicit . 通常,分配和转换是多余的:如果您接受隐式转换(使用U::operator T() const -或使用T::T(const U&)从- T::T(const U&) ),则不必提供其他分配而不是默认值,并且如果您要隐式地进行异构分配,则必须不提供转换,或者最多不要使它们explicit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 多个转换运算符导致赋值运算符歧义 - Multiple conversion operators causing assignment operator ambiguity 转换运算符和构造函数的歧义 - ambiguity with conversion operator and constructor 隐式运算符 != 解决运算符 == 中的歧义 - Implicit operator != resolving ambiguity in operators == Clang模糊与自定义转换运算符 - Clang ambiguity with custom conversion operator 显式转换运算符模板的优先级和模糊性 - Priority and ambiguity of explicit conversion operator templates 转换运算符重载歧义,编译器不同 - conversion operator overloading ambiguity, compilers differ C++中重载函数和多个转换运算符的歧义,编译器不同意 - Overloaded function and multiple conversion operators ambiguity in C++, compilers disagree 三元运算符应用于 class with conversion operator and delete constructor 导致歧义 - Ternary operator applied to class with conversion operator and delete constructor causes ambiguity 涉及模板化转换运算符和隐式复制构造函数的歧义 - Ambiguity involving templated conversion operator and implicit copy constructor 为什么我的显式构造函数会为我的转换运算符创建这种歧义? - Why is my explicit constructor creating this ambiguity for my conversion operator?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM