virtual

The impact of virtual on the use of member of class template

依然范特西╮ 提交于 2019-12-09 04:39:40
问题 I (vaguely) know that a template is not instantiated if it is not used . For example, the following code will compile fine even though T::type doesn't make sense when T = int . template<typename T> struct A { void f() { using type = typename T::type; } }; A<int> a; //ok It compiles because f() is not used , so it is not instantiated — thus the validity of T::type remains unchecked. It doesn't matter if some other member function g() calls f() . template<typename T> struct A { void f() { using

C#: Virtual Function invocation is even faster than a delegate invocation?

一曲冷凌霜 提交于 2019-12-09 04:39:17
问题 It just happens to me about one code design question. Say, I have one "template" method that invokes some functions that may "alter". A intuitive design is to follow "Template Design Pattern". Define the altering functions to be "virtual" functions to be overridden in subclasses. Or, I can just use delegate functions without "virtual". The delegate functions is injected so that they can be customized too. Originally, I thought the second "delegate" way would be faster than "virtual" way, but

virtual base class and initialization lists [duplicate]

Deadly 提交于 2019-12-09 03:41:40
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: gcc c++ virtual inheritance problem Hi all, I'm wondering about how the compiler would handle different initialization values when using multiple inheritance from a virtual base class. Consider the notorious 'diamond of dread' inheritance scheme: Base / \ / \ D1 D2 \ / \ / Join In order to avoid having two copies of Base in Join , I use virtual inheritance for D1 and D2 (see e.g. here). Now, lets say Base is not

Rhino mock an abstract class w/o mocking its virtual method?

半腔热情 提交于 2019-12-09 03:13:34
问题 Can I execute the body of a virtual method that lives on an abstract class which has been mocked using Rhino Mocks? To be clear, I'm not trying to mock the behavior of the virtual method. I'm trying to /test/ the virtual method (on the mocked class). Is this idea a blatant misuse of Rhino Mocks? 回答1: Yes, that should be absolutely fine. I can't say I've tried it, but I'd be very surprised if it failed. EDIT: I suspect you want the PartialMock method. Here's an example: using System; using

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

梦想的初衷 提交于 2019-12-08 22:25:39
问题 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

C++ return type when I don't know if it's temporary

拥有回忆 提交于 2019-12-08 16:22:21
问题 Suppose that Foo is a rather large data structure. How should I write a const virtual function that returns an instance of Foo , if I don't know whether the inherited classes will store the instance of Foo internally; thus, allowing a return by reference. If I can't store it internally, my understanding is I can't return a const reference to it because it will be a temporary. Is this correct? The two options are: virtual Foo foo() const { ... } virtual Foo const & foo() const { ... } Here's a

How does the compiler internally solve the diamond problem in C++?

て烟熏妆下的殇ゞ 提交于 2019-12-08 15:29:39
问题 We know that we can solve the diamond problem using virtual inheritance. For example: class Animal // base class { int weight; public: int getWeight() { return weight;}; }; class Tiger : public Animal { /* ... */ }; class Lion : public Animal { /* ... */ }; class Liger : public Tiger, public Lion { /* ... */ }; int main() { Liger lg ; /*COMPILE ERROR, the code below will not get past any C++ compiler */ int weight = lg.getWeight(); } When we compile this code we will get an ambiguity error.

C++ virtual variable in inheritance class hierarchy

℡╲_俬逩灬. 提交于 2019-12-08 14:42:47
问题 I have a template class hierarchy, ___ Class (ClassA) | AbstractClass_____ |___ Class (ClassB) in classA and ClassB, I have a const NullPosition of a templated type, which is different in ClassA and ClassB. In class classA and ClassB I have to do some operation which are dependant on the value of the NullPosition. Now I need to do some operations depending on the value on NullPosition, but I am having hard time since the variable are different type and values. To be more specific NullPosition

何时使用虚拟析构函数?

旧时模样 提交于 2019-12-08 14:23:28
我对大多数面向对象理论有扎实的了解,但令我困惑的一件事是虚拟析构函数。 我以为无论链中的每个对象是什么,析构函数总是被调用。 您打算何时将它们虚拟化?为什么? #1楼 我喜欢考虑接口和接口的实现。 在C ++中,接口是纯虚拟类。 析构函数是接口的一部分,有望实现。 因此,析构函数应该是纯虚拟的。 构造函数呢? 构造函数实际上不是接口的一部分,因为对象总是显式实例化的。 #2楼 虚拟构造函数是不可能的,但虚拟析构函数是可能的。 让我们尝试一下... #include <iostream> using namespace std; class Base { public: Base(){ cout << "Base Constructor Called\n"; } ~Base(){ cout << "Base Destructor called\n"; } }; class Derived1: public Base { public: Derived1(){ cout << "Derived constructor called\n"; } ~Derived1(){ cout << "Derived destructor called\n"; } }; int main() { Base *b = new Derived1(); delete b; } 上面的代码输出以下内容:

Using virtual com port on windows mobile

白昼怎懂夜的黑 提交于 2019-12-08 10:42:06
问题 I have a windows mobile device which is running windows CE 5.0. I want to now connect a hardware to the USB port and communicate with the hardware by configuring it as a virutal com port and writing/reading bytes to/from it. Do i have to write a virual com driver for the windows mobile device? Please note that the hardware device that is just a device which sends/receives bytes.. Can i simply use the SerialPort class of .net framework? How will i know the COM port to which the device is