我可以覆盖运算符的重载并返回不同的类型吗?

发布于 2024-09-28 01:58:21 字数 636 浏览 11 评论 0原文

class A{
  public: 
    virtual char &operator[](int);
  protected:
    ..
};
class B:A{
  public:
    A* &operator[](int);
  protected:
}

当我重载运算符的重载时,我可以更改返回类型吗?

谢谢!

//编辑 好的,既然我们已经确定这行不通,我该如何解决呢?

假设我有 A、B、C 和 D 类。

class A{
  public: 
  private:
    char &operator[](int);
  protected:
    ..
};
class B:A{
  public: 
    virtual char &operator[](int);
};
class C: A{
  public:
  private:
    A::&operator[](int);
}
class D: A{
  public:
  private:
     A::&operator[](int);
}

我可以做这样的事情吗?如果是这样,这是正确的语法吗?

class A{
  public: 
    virtual char &operator[](int);
  protected:
    ..
};
class B:A{
  public:
    A* &operator[](int);
  protected:
}

Can I change the return type when I overload an overload of an operator?

thanks!

//EDIT
Okay, so now that we established that this wont work how can I build a work around?

Lets say I have classes A,B,C, and D.

class A{
  public: 
  private:
    char &operator[](int);
  protected:
    ..
};
class B:A{
  public: 
    virtual char &operator[](int);
};
class C: A{
  public:
  private:
    A::&operator[](int);
}
class D: A{
  public:
  private:
     A::&operator[](int);
}

Can I do something like this? If so is this the correct syntax?

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

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

发布评论

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

评论(4

极致的悲 2024-10-05 01:58:21

不是这样的,不。

重写的返回类型必须与

  • 被重写的虚函数的返回类型相同,或者是
  • 被重写的虚函数的返回类型的派生类(这称为“协变返回类型”)。

因此,如果虚拟 A::operator[] 返回 A*,则 B::operator[] 重写可能会返回 <代码>B*。

Not like this, no.

The return type of an override must be either

  • the same type as the return type of the virtual function being overridden, or
  • a derived class of the return type of the virtual function being overridden (this is called a "covariant return type").

So, if a virtual A::operator[] returned an A*, then a B::operator[] override could return a B*.

花开浅夏 2024-10-05 01:58:21

多态函数不能在不同类中返回不同类型的原因并不是因为 C++ 委员会中的某个人认为这是“禁忌”,而是因为任何使用该函数返回值的代码都无法编译。

通过创建继承层次结构,您可以通过基指针或引用访问派生对象:

class A
{
public:
    virtual char operator[](int);
};
class B : public A
{
public:
    virtual char operator[](int);
};

A *a;

std::cout << "Do you want to make an A or a B?";

char type;
std::cin >> type;

if (type == 'A')
    a = new A();
else
    a = new B();

char c = (*a)[0];

请注意,在最后一行,编译器不会知道 a 指向什么类型的对象,因为这是在运行时确定的。这很好,因为无论 a 指向什么类型的对象,operator[] 仍然会返回一个字符。但是,如果允许该运算符在 B 类中返回不同类型怎么办?

class Sequence
{
    ...
};

class A
{
public:
    virtual char operator[](int);
};
class B : public A
{
public:
    virtual Sequence operator[](int);
};

A *a = new B();
char c = (*a)[0];

显然,当 aB 类型的对象时,最后一行没有任何意义。在这种情况下,您尝试将序列分配给角色。同样,如果 aA 类型的对象,则 Sequence c = (*a)[0]; 就没有意义。

The reason that a polymorphic function can't return different types in different classes isn't because someone on the C++ committee decided that it was "taboo", but because any code that used that function's return value couldn't compile.

By creating an inheritance heirarchy, you're able to access derived objects through a base pointer or reference:

class A
{
public:
    virtual char operator[](int);
};
class B : public A
{
public:
    virtual char operator[](int);
};

A *a;

std::cout << "Do you want to make an A or a B?";

char type;
std::cin >> type;

if (type == 'A')
    a = new A();
else
    a = new B();

char c = (*a)[0];

Note that on the last line, the compiler won't know what type of object a is pointing to, since that's determined at runtime. This is fine, because no matter what type of object a is pointing to, operator[] is still going to return a character. But what if that operator were allowed to return a different type in class B?

class Sequence
{
    ...
};

class A
{
public:
    virtual char operator[](int);
};
class B : public A
{
public:
    virtual Sequence operator[](int);
};

A *a = new B();
char c = (*a)[0];

Obviously, that last line makes no sense when a is an object of type B. In that case, you're trying to assign a Sequence to a character. Likewise, Sequence c = (*a)[0]; wouldn't make sense if a were an object of type A.

梦归所梦 2024-10-05 01:58:21

正确的解决方案是使 A 成为类 CD 的成员,而不是基类。由于 CD 需要为公共方法使用不同的签名,因此它们显然不等于每个上下文中的 A,因此尝试在这里使用继承确实没有意义。

The proper solution is to make A a member of classes C and D rather than a base class. Since C and D need a different signature for a public method, they're clearly not equivalent to an A in every context, so trying to use inheritance here really makes no sense.

呆° 2024-10-05 01:58:21

好的,看看您关于序列 (A)、GeneSequence (B) 和 GenomeSequence (C) 的最后评论,

您希望操作员有时返回一个char,有时会返回一个Sequence

那么,如您所见,超类A声明的类型是不正确的>&运算符 [] 作为 char

它应该使用 template 作为类型参数,以指定它是 char 的集合还是 Sequence 的集合,对吧?

OK looking at your last comment about Sequence (A), GeneSequence (B) and GenomeSequence (C)

you want the operator to sometimes return a char, and sometimes return a Sequence

well then, as you can see, it is incorrect for the superclass A to declare the type of &operator [] as char.

It should use a template as a type parameter, to specify whether it is a collection of chars or Sequences, right?

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