MFC VC ++中的虚拟构造函数 [英] Virtual constructor in MFC VC++

查看:98
本文介绍了MFC VC ++中的虚拟构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以有虚拟构造函数?如果是,怎么办?如果不是,为什么不能呢?

Is it possible to have virtual constructor? If yes, how? If not, Why not possible?

推荐答案

C ++不支持虚拟构造函数. 为什么"不是很正确的问题. C ++的意识形态大相径庭,这就是原因.您可能想读一读C ++类,这是一本可以读到它的书.

(顺便说一下,许多C ++开发人员,大多是那些不熟悉其他语言的开发人员,提供了关于原理上不可能或没有意义的详细解释.这种解释是不正确的,因为在其他语言中,虚拟构造函数确实存在,可以使它们变得完美.对于某些应用程序来说,它是有意义的,并且非常重要;同样,它代表虚拟静态方法:它们在C ++中不存在,但是在其他语言中却很有意义并且很重要,例如Borland Object Pascal和Delphi Pascal.元类的概念,它是实例是类,而不是类的实例.)

回答后续问题.

在C ++开发中,最典型的解决方法是使用 class factory . C ++意识形态专家通常会说为什么要在语言中引入一些可以使用可用语言功能开发的内容".这种方法本身非常健康.当然,解决方法不是创建虚拟方法,而是提供一些功能来帮助实现虚拟方法的设计目标. 工厂是一种面向对象的设计模式( http://en.wikipedia.org/wiki/Design_pattern_(computer_science) [ ^ ])分类为娱乐模式( http://en.wikipedia.org/wiki/Creational_pattern [ ^ ]).这是模式描述: http://en.wikipedia.org/wiki/Factory_(software_concept) [ ^ ].该模式可以实现为单独的类,也可以实现为某些基类的方法,以返回某些派生类的构造实例.毕竟,工厂方法可以是虚拟的,与真实"构造函数不同.这种方法的应用之一是从包含对象图的持久表示的流中构造多态容器的对象.

—SA
C++ does not support virtual constructors. "Why" is not quite correct question. The ideology of C++ is quite different, that''s why. You may want to read on C++ classes is some book to get into it.

(By the way, many C++ developers, mostly those who are not familiar with other languages provide detailed explanation of why it is impossible in principle or makes no sense. Such explanations are not correct, because in other languages virtual constructors do exist, make perfect sense and very important for some application. The same stands for virtual static methods: they do not exist in C++ but make perfect sense and important in other languages. One example is Borland Object Pascal and Delphi Pascal. The notion of virtual constructor is remotely related to the concept of meta-class, which is something which instance is a class, not instance of class.)

Answering a follow-up question.

In C++ development, the most typical work around is using class factory. C++ ideologists usually say "why introducing in language something that can be developed using available language features". This approach itself is very healthy. Of course, the work-around is not creating virtual method, but providing some functionality helping to reach some goals virtual methods are designed for. Factory is a an object-oriented design pattern (http://en.wikipedia.org/wiki/Design_pattern_(computer_science)[^]) classified as a creational pattern (http://en.wikipedia.org/wiki/Creational_pattern[^]). This is the pattern description: http://en.wikipedia.org/wiki/Factory_(software_concept)[^]. The pattern can be implemented as a separate class or as a method of some base class returning constructed instances of some derived classes. After all, factory methods can be virtual, unlike "real" constructors. One of the applications of such approach is constructing of the object of polymorphous container out of stream containing persistent representation of the object graph.

—SA


这篇关于MFC VC ++中的虚拟构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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