virtual

C++构造函数和析构函数调用虚函数时都不会使用动态联编

六眼飞鱼酱① 提交于 2019-11-29 17:11:05
可以,虚函数底层实现原理(但是最好不要在构造和析构函数中调用) 可以,但是没有动态绑定的效果,父类构造函数中调用的仍然是父类版本的函数,子类中调用的仍然是子类版本的函数。 effictive c++第九条,绝不在构造和析构过程中调用virtual,因为构造函数中的base的虚函数不会下降到derived上。而是直接调用base类的虚函数。绝不在构造和析构函数中调用virtual函数: a) 如果有继承,构造函数会先调用父类构造函数,而如果构造函数中有虚函数,此时子类还没有构造,所以此时的对象还是父类的,不会触发多态。更容易记的是基类构造期间,virtual函数不是virtual函数。 b) 析构函数也是一样,子类先进行析构,这时,如果有virtual函数的话,子类的内容已经被析构了,C++会视其父类,执行父类的virtual函数。 c) 总之,在构造和析构函数中,不要用虚函数。如果必须用,那么分离出一个Init函数和一个close函数,实现相关功能即可。 原文链接:https://blog.csdn.net/chen134225/article/details/81564972 第一个理由是概念上的。 在概念上,构造函数的工作是生成一个对象。在任何构造函数中,可能只是部分形成对象——我们只能知道基类已被初始化,但并不能知道哪个类是从这个基类继承来的。然而,虚函数在继承层次上是“向前

[C++]Covariant return types

女生的网名这么多〃 提交于 2019-11-29 14:57:18
I have a VectorN class, and a Vector3 class inherited from VectorN (which can handle cross products for example). I have trouble determining the return types of the different operators. Example: class VectorN { public: VectorN(){}; virtual VectorN operator*(const double& d) {.....}; std::vector<double> coords; }; class Vector3 : public VectorN { public: Vector3(){}; virtual Vector3 operator*(const double& d) {....}; }; This particular example produces a C2555 error: 'Vector3::operator *': overriding virtual function return type differs and is not covariant from 'VectorN::operator *', see

C# enum in interface/base class?

十年热恋 提交于 2019-11-29 13:10:17
问题 i have problem with enum I need make a enum in base class or interface (but empty one) class Base { public enum Test; // ??? } and after make diffrent enums in some parent classes class Parent1 { public enum Test {A, B, C}; } class Parent2 { public enum Test {J, H, K}; } and now i have next class with method when i have to use enum class Test<T> { public void Foo(Test enum) { int value = (int) enum; // ... } } It's there any way to do something like that ? If not i have to use static ints in

Bluetooth HID Device & iOS textFields

时间秒杀一切 提交于 2019-11-29 13:08:33
We are using a BT device, which acts as a keyboard, to talk to the iPad. We want this bluetooth device to talk to 1 field in our app. (Which is on it's own view) All other textFields/areaFields we want to display the virtual keyboard. Is this possible? Whenever the BT device is paired it disabled the on screen keyboard. We thought of turning bluetooth off before the view with the 1 field that needs its input from the device is displayed and then turning it back on when the view is removed, but this has 2 problems. The iPad will not automatically connect to the device and there is no way to

In an abstract class constructor, why I do need to call a constructor of a virtual base that will never to called?

戏子无情 提交于 2019-11-29 13:05:28
I face the well known "dreaded" diamond situation : A / \ B1 B2 \ / C | D The class A has, say the constructor A::A(int i) . I also want to forbid a default instantiation of a A so I declare the default constructor of A as private . The classes B1 and B2 are virtually derived from A and have some constructors and a protected default constructor. [edit] The constructors of B1 and B2 don't call the default constructor of A . [reedit] The default constructors of B1 and B2 don't call the default constructor of A either. [reedit] [edit] The class C is an abstract class and has some constructors

open the Windows virtual keyboard in a Java program

你说的曾经没有我的故事 提交于 2019-11-29 13:01:18
I would like to create an event in a button. When I click in the button, I would like to open the Windows virtual keyboard. Can you help me with the code? Thank you for your collaboration. Best regards. I think it would be as simple as this: Runtime.getRuntime().exec("osk"); You can use getRuntime to execute it: import java.io.IOException; public class ShowVirtualKeyboard{ public static void main(String argv[]) throws IOException { String sysroot = System.getenv("SystemRoot"); Process proc = Runtime.getRuntime().exec(sysroot + "/system32/osk.exe"); } } and you can call proc.destroy() to get

Does C++ final imply final in all aspects?

怎甘沉沦 提交于 2019-11-29 12:15:00
问题 C++11 added final. Finally! I understand final does two things: Makes a class non-inheritable. Makes (virtual) functions in a class non-overridable (in a derived class). Both of these seem independent of each other. But take for example the following: class Foo { public: virtual void bar() { //do something unimportant. } }; class Baz final : public Foo { public: void bar() /*final*/ override { //do something more important than Foo's bar. } }; From above, I believe Baz being final , I should

Windows Azure - Virtual Network

ぃ、小莉子 提交于 2019-11-29 11:21:40
Windows Azure - Virtual Network 其实Virtual Network很早就出现在Windows Azure的家族里面,但是小弟一直处于兵荒马乱的情况下,实在拨不出一些时间来好好的玩一玩与纪录 ( 迷之声 : 是在打电动吧!? ),而称著这段时间,在看一些SQL Server in Virtual Machine的数据的时候,就顺便也把Virtual Network给k一下了。 Virtual Network,可以说是一个重要的服务,为什么!?,因为天与地就是靠他来联系,来达到混合云的架构;其实说到这,大家应该就能体会到,( 其实看到名称应该就可以联想到了吧0 0.. )。是的,Virtual Network就是架构出一个虚拟的网络,来让天与地都能加入此网络,形成一个独立且安全的网段!!!当然,这个服务基本上是和Virtual Machine最紧密的整合( 因为都叫做Virtual!?,好啦,我知道很冷… ),我们可以透过此服务,让我们在Azure上的VM,和本地端的Server整合起来,就像区往依样!! ok~说了那么多,我们还是来实践看看吧,喔,对了,今天这个实践,基本上还不会有整合的例子,这边只是纯粹的在教大家设定喔!!另外,这篇的基础主要来自于这边,有兴趣的也可以参考原文。 好的,首先,一开始,我们就直接进入管理界面,建立一个Virtual

Virtual Memory

安稳与你 提交于 2019-11-29 11:18:06
Most of the literature on Virtual Memory point out that the as a Application developer,understanding Virtual Memory can help me in harnessing its powerful capabilities. I have been involved in developing applications on Linux for sometime but but didn't care about Virtual Memory intricacies while I code. Am I missing something? If so, please shed some light on how I can leverage the workings of Virtual Memory. Else let me know if am I not making sense with the question! Well, the concept is pretty simple actually. I won't repeat it here, but you should pick up any book on OS design and it will

Pure virtual functions in C++11

╄→гoц情女王★ 提交于 2019-11-29 10:37:58
问题 In C++98, the null pointer was represented by the literal 0 (or in fact any constant expression whose value was zero). In C++11, we prefer nullptr instead. But this doesn't work for pure virtual functions: struct X { virtual void foo() = nullptr; }; Why does this not work? Would it not make total sense? Is this simply an oversight? Will it be fixed? 回答1: Because the syntax says 0 , not expression or some other non-terminal matching nullptr . For all the time only 0 has worked. Even 0L would