C++ 对抽象类的引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5019046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:11:26  来源:igfitidea点击:

reference to abstract class

c++referenceabstract

提问by lital maatuk

What does it mean when there is a reference to an abstract class? I found it in code and I can't understant it.

当有对抽象类的引用时,这意味着什么?我在代码中找到了它,我无法理解它。

I thought that an abstract class can't be instantiated. How can you give it a reference?

我认为抽象类不能被实例化。你怎么给它一个参考?

回答by Jeremiah Willcock

A reference to an abstract class is just like a pointer to an abstract class: it needs to reference an object of some non-abstract subclass of the abstract class. You can use a reference like that to call virtual methods on the referenced class using the .syntax, in a way similar to a pointer to an interface in Java.

对抽象类的引用就像是指向抽象类的指针:它需要引用抽象类的某个非抽象子类的对象。您可以使用类似的引用来使用.语法调用被引用类上的虚拟方法,其方式类似于 Java 中指向接口的指针。

回答by Johann Gerell

An abstract class is designed to be derived from. The Liskov substitution principle roughly states that anything that uses the abstract parts of types derived from an abstract base should work equally well using the base polymorphically. That means a reference or pointer to the base should be used.

抽象类旨在派生自。Liskov 替换原则粗略地指出,任何使用从抽象基派生的类型的抽象部分的任何事物都应该以多态方式使用基类同样好。这意味着应该使用指向基的引用或指针。

回答by James

class Abstract
{
public:
  virtual void foo() = 0;
};

class Implementation : public Abstract
{
public:
  void foo() { std::cout << "Foo!" << std::endl; }
};

void call_foo(Abstract& obj) { obj.foo(); } 

int main()
{
  Abstract *bar = new Implementation();

  call_foo(*bar);

  delete bar;
}

baris a pointerto an abstract class. It can be dereferenced using the *operator and passed as a referenceinto call_foo, because that is what call_foois asking for (Abstract*would be asking for a pointer, whereas Abstract&is asking for a reference).

bar是一个pointer抽象类。它可以使用*运算符取消引用并作为referenceinto传递call_foo,因为这就是所call_foo要求的(Abstract*将要求一个指针,而Abstract&要求一个引用)。

In the above, the reference to the abstract class is passed, and when foo()is called using the .notation (instead of the pointer ->notation), it prints Foo!, because that is what the Implementationdoes.

在上面,抽象类的引用被传递,当foo()使用.符号(而不是指针->符号)调用时,它打印Foo!,因为这就是它Implementation所做的。

Hope this helps.

希望这可以帮助。

回答by bartgol

References in c++ behave (almost) like hidden pointers. In particular, the same polymorphic behavior you can get with a pointer, you can achieve it with a reference. That is, the following are (almost) equivalent

C++ 中的引用行为(几乎)像隐藏的指针。特别是,您可以使用指针获得相同的多态行为,您可以使用引用来实现它。也就是说,以下(几乎)是等效的

int *i = &a;
int &j = a;

assuming a was an integer defined somewhere in the previous lines. Following occurrences of the reference j, are perfectly equivalent to occurrences of (*i). The main difference is that a reference doesn't give you the pain of memory management, while a pointer does (it is your responsibility to handle new(s) and delete(s)). Also, a pointer doesn't have to point to something, while a reference can't exists if it's not referring to anything. Other than that, you can consider them to behave the same way.

假设 a 是在前几行某处定义的整数。以下引用 j 的出现完全等同于 (*i) 的出现。主要区别在于引用不会给您带来内存管理的痛苦,而指针会给您带来麻烦(处理 new(s) 和 delete(s) 是您的责任)。此外,指针不必指向某物,而如果不指向任何东西,则引用不能存在。除此之外,您可以认为它们的行为方式相同。

Therefore, it's absolutely legal to have a reference to an abstract object. You will find it often in functions signatures, where the polymorphic behavior can be achieved either with references or pointers. But references give a lighter syntax, like the following piece of code shows

因此,引用抽象对象是绝对合法的。您会经常在函数签名中找到它,其中的多态行为可以通过引用或指针来实现。但是引用提供了更轻的语法,如下面的代码所示

A a;
A* ptr = &a;
A& ref = a;
ref();
ptr->operator()();
(*ptr)();

assuming the class A overloads operator ().

假设 A 类重载operator ()