对抽象类的引用
当引用抽象类时,这意味着什么? 我在代码中找到了它,但我无法理解它。
我认为抽象类不能被实例化。怎样才能给它一个参考呢?
What does it mean when there is a reference to an abstract class?
I found it in code and I can't understand it.
I thought that an abstract class can't be instantiated. How can you give it a reference?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对抽象类的引用就像指向抽象类的指针一样:它需要引用抽象类的某个非抽象子类的对象。您可以使用类似的引用来使用
.
语法调用被引用类上的虚拟方法,其方式类似于 Java 中指向接口的指针。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.抽象类被设计为派生类。里氏替换原则粗略地指出,任何使用从抽象基派生的类型的抽象部分的东西都应该使用基多态性地工作得同样好。这意味着应该使用指向基数的引用或指针。
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.
bar
是一个指向抽象类的指针
。可以使用*
运算符取消引用它,并将其作为引用
传递到call_foo
中,因为这就是call_foo
的作用请求(Abstract*
将请求一个指针,而Abstract&
则请求一个引用)。在上面,传递了对抽象类的引用,并且当使用
.
表示法(而不是指针->< 调用
foo()
时) /code> 表示法),它会打印Foo!
,因为这就是Implementation
的作用。希望这有帮助。
bar
is apointer
to an abstract class. It can be dereferenced using the*
operator and passed as areference
intocall_foo
, because that is whatcall_foo
is asking for (Abstract*
would be asking for a pointer, whereasAbstract&
is asking for a reference).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 printsFoo!
, because that is what theImplementation
does.Hope this helps.
C++ 中的引用的行为(几乎)类似于隐藏指针。特别是,您可以通过指针获得相同的多态行为,您可以通过引用来实现它。也就是说,
假设 a 是前面几行中某处定义的整数,则以下内容(几乎)是等效的。随后出现的引用 j 完全等同于 (*i) 的出现。主要区别在于,引用不会给您带来内存管理的痛苦,而指针则会带来内存管理的痛苦(您有责任处理 new(s) 和 delete(s))。此外,指针不必指向某些东西,而如果引用不指向任何东西(1),那么它就不会存在。除此之外,您可以认为它们的行为方式相同。
因此,引用抽象对象是绝对合法的。您经常会在函数签名中发现它,其中可以通过引用或指针来实现多态行为。但引用提供了更轻量级的语法,如下面的代码所示,
假设类 A 重载了
operator ()
。(1) 嗯,你可以有悬空引用,就像指针一样:
但是如果不指定它们可以引用的东西,就无法创建它们。
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
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 (1). Other than that, you can consider them to behave the same way.
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
assuming the class A overloads
operator ()
.(1) Well, you can have dangling references, just like pointers:
but they cannot be created without assigning something they can refer to.