virtual

c++设计一个不能被继承的类,为什么必须是虚继承?原因分析

房东的猫 提交于 2019-12-10 17:55:13
用C++实现一个不能被继承的类(例1) #include <iostream> using namespace std; template <typename T> class Base{ friend T; private: Base(){ cout << "base" << endl; } ~Base(){} }; class B:virtual public Base<B>{ //一定注意 必须是虚继承 public: B(){ cout << "B" << endl; } }; class C:public B{ public: C(){} //继承时报错,无法通过编译 }; int main(){ B b; //B类无法被继承 //C c; return 0; } 类Base的构造函数和析构函数因为是私有的,只有Base类的友元可以访问,B类在继承时将模板的参数设置为了B类,所以构造B类对象时们可以直接访问父类(Base)的构造函数。 为什么必须是虚继承(virtual)呢? 参见 c++Primer 4th 第17.3.7节 特殊的初始化语义 通常每个类只初始化自己的直接基类,但是在虚继承的时候这个情况发生了变化,可能导致虚基类被多次初始化,这显然不是我们想要的。(例2: AA,AB都是类A的派生类,然后类C又继承自AA和AB,如果按之前的方法会导致C里面A被初始化两次

Is it safe if a template contains virtual function?

冷暖自知 提交于 2019-12-10 17:33:11
问题 Early binding for template and late binding for virtual function. Therefore, is it safe if a template contains virtual function? template<typename T> class base { public: T data; virtual void fn(T t){} }; 回答1: It is completely safe. Once you instantiate the class template, it becomes normal class just like other classes. template<typename T> class base { public: T data; virtual void fn(T t){} }; class derived : base<int> { public: virtual void fn(int t){} //override }; base<int> *pBase = new

Address of (&) gives compiler generated address or loader generated address?

☆樱花仙子☆ 提交于 2019-12-10 17:28:12
问题 int a; printf("address is %u", &a); Which address is this..? I mean is this a compiler generated address i.e. virtual address or the loader given physical address in the RAM..? As it prints different address every time, I guess it must be address in the RAM. Just want to make sure. Please provide any links which give reference to your answer. 回答1: The correct answer is: "it depends." (The printf should use the "%p" directive, and cast the address to "void *", for the sake of well-defined-ness

How do I add virtual hosts the right way while running WHM?

大憨熊 提交于 2019-12-10 17:26:55
问题 I'm running a dedicated server separating accounts for my clients with WHM and CentOS 5. One of my clients has asked me to install subversion, and have the repository stored beneath the webroot. repo's true folder will be in "/home/theirfolder/svn" repo will be accessed through a subdomain on "svn.theirdomain.com" I know that the regular way to do this is to set up a virtual host in Apache that handles the redirect. The problem is that WHM seems to overtake the whole virtual hosting process,

C++: Deprecation warning when overriding a deprecated virtual method

☆樱花仙子☆ 提交于 2019-12-10 17:08:01
问题 I have an pure virtual class that has a pure virtual method that should be const , but unfortunately is not. This interface is in a library, and the class is inherited by several other classes, in separate projects. I'm trying to make this method const without breaking compatibility (at least for some time), but I cannot find a way to produce a warning when the non-const method is overloaded. The following is an example of what I was able to produce so far: Stage 0 : Before the change. Only

Does “virtual base class in the case of multilevel inheritance” have significance

痞子三分冷 提交于 2019-12-10 16:32:33
问题 Consider the following sample codes which shows multilevel inheritance: Case1 : Here the class derived1 is derived from the class base through virtual inheritance and the class derived2 is derived from the class derived1 directly. class base { }; class derived1 : virtual public base { }; class derived2 : public derived1 { }; Case2 : Same as Case1 except that no virtual inheritance is involved class base { }; class derived1 : public base // no virtual inheritance { }; class derived2 : public

C# calling overridden subclass methods without knowledge that it's a subclass instance

不羁的心 提交于 2019-12-10 15:46:57
问题 I have a base class with a virtual method, and multiple subclasses that override that method. When I encounter one of those subclasses, I would like to call the overridden method, but without knowledge of the subclass. I can think of ugly ways to do this (check a value and cast it), but it seems like there should be an in-language way to do it. I want the List to contain multiple subclasses within the same list, otherwise obviously I could just make a List. EDIT: Fixed the comment in the code

What is the most concise yet accurate way to describe what a virtual function is in C++?

我们两清 提交于 2019-12-10 13:36:49
问题 Being asked to describe what a virtual function is seems to be one of the most common questions in interviews assessing basic C++ knowledge. However, after several years programming C++, I still have the uncomfortable feeling that I don't really understand how best to define what they are. If I consult wikipedia, I see the definition of virtual function is: "In object-oriented programming, a virtual function or virtual method is a function or method whose behaviour can be overridden within an

Is there a way to flag (at compile time) “overridden” methods whose signatures don't match base signature?

偶尔善良 提交于 2019-12-10 13:19:28
问题 Basically, I want the C# compiler functionality of its override keyword in my C++ code. class Base { virtual int foo(int) const; }; class Derived : public Base { virtual int foo(int); // wanted to override Base, but forgot to declare it const }; As we all know, the above code will compile fine, but yield some strange runtime behavior. I would love my C++ compiler to catch my poor implementation with something like C#'s override keyword. Are there any keywords like "override" being introduced

Simulink - How to run a simulation in a simulation?

烂漫一生 提交于 2019-12-10 12:16:46
问题 I am running a simulation that contains among other things a subsystem (or referenced model) that is a simulation by itself and that needs to be run fully at certain times before resuming the main simulation. Basically, at time t the main simulation needs the outputs of the subsystem. The subsystem is then triggered and runs a simulation for 6 seconds (the subsystem simulation is time dependent). Then the main simulation uses the outputs of the subsystem. The problem here is that when the