virtual

Does C++ virtual function call on derived object go through vtable?

放肆的年华 提交于 2019-11-28 13:41:43
In the following code, it calls a virtual function foo via a pointer to a derived object. Will this call go through the vtable or will it call B::foo directly? If it goes via a vtable, what would be a C++ idiomatic way of making it call B::foo directly? I know that in this case I am always pointing to a B . Class A { public: virtual void foo() {} }; class B : public A { public: virtual void foo() {} }; int main() { B* b = new B(); b->foo(); } Yes, it will use the vtable (only non-virtual methods bypass the vtable). To call B::foo() on b directly, call b->B::foo() . Most compilers will be smart

消息队列rabbitmq

扶醉桌前 提交于 2019-11-28 13:28:14
AMQP:advanced message queuing protocol 协议模型 publisher consumer broker virtual host exchange message queue binding channel channel: 几乎所有的操作都在channel中进行,客户端可建立多个channel,每个channel代表一个会话任务 message: 由properties和body组成。properties可以对消息进行修饰,比如消息的优先级、延迟等高级特性; body就是消息体的内容 virtual host: 逻辑隔离,一个virtual host可以有若干个exchange和queue,同一个virtual host不能有相同名称 的exchange和queue exchange: 接受消息,根据路由键转发消息到绑定的队列 binding: exchange和queue中的虚拟连接,binding中可以包含routing key routing key: queue: 端口号 amqp : 5672 cluster: 25672 management: 15672 入门demo 生产端 connectionfactory,setHost、port、virtualHost connection connection .

C#: What are virtual events and how can they be used?

徘徊边缘 提交于 2019-11-28 13:16:37
How does a virtual event work? How would you override it? How would that work? And in what cases would you do that? Would it for example be an ok replacement for protected OnEvent methods? So that inheriting classes could just override the event and raise it directly? Or would that be wrong or just not work? The MSDN says this about it: An event can be marked as a virtual event by using the virtual keyword. This enables derived classes to override the event behavior by using the override keyword. An event overriding a virtual event can also be sealed, which specifies that for derived classes

Why does this code crash at the places mentioned?

ぃ、小莉子 提交于 2019-11-28 12:26:33
Can you please elaborate why this code crashes at the places mentioned? I am a bit stumped on this. I guess that it has got something to do with sizeof(int) but I am not so sure. Can anybody explain? class Base { public: virtual void SomeFunction() { printf("test base\n"); } int m_j; }; class Derived : public Base { public: void SomeFunction() { printf("test derive\n"); } private: int m_i; }; void MyWonderfulCode(Base baseArray[]) { baseArray[0].SomeFunction(); //Works fine baseArray[1].SomeFunction(); //Crashes because of invalid vfptr baseArray[2].SomeFunction(); //Crashes because of invalid

How to design around the limitation that templated member functions can't be virtual

别说谁变了你拦得住时间么 提交于 2019-11-28 12:18:33
I'm running into a design issue where (in C++) I'd like a templated member function (of a non-template class) to be virtual and am wondering if there is a good, elegant way around the issue. The scenario goes, I have machines that process generic items . I use an abstract base class for machines with a virtual process(Item) function so that each machine can define their own unique processing method. The problem is that the items are also "generic" in that they expose certain interfaces for how they can be processed. For reasons (mainly for performance...no vtable overhead), I'd like to use

Multiple dispatch solution with full maintainability

杀马特。学长 韩版系。学妹 提交于 2019-11-28 11:51:26
Can someone think of a good way to implement multiple dispatch with something like the Object::foo overloads below? class A { public: virtual void accept (Visitor&) = 0; }; class B : public A { virtual void accept (Visitor&) override; }; class C : public A { virtual void accept (Visitor&) override; }; class D : public A { virtual void accept (Visitor&) override; }; class Object { public: virtual double foo (A*, A*) { std::cout << "Object::foo A,A\n"; return 3.14; } virtual double foo (B*, B*) { std::cout << "Object::foo B,B\n"; return 3.14; } virtual double foo (B*, C*) { std::cout << "Object:

Calling derived class function from base class

拈花ヽ惹草 提交于 2019-11-28 09:09:50
class base { public: virtual void start(); virtual void stop(); void doSomething() { start(); .... stop(); } } class derived : public base { public: void start(); void stop(); } But when I call doSomething() in the derived class it is using it's own definition of Start() and Stop() - not the derived ones. I don't want to rewrite doSomething() in the derived class because it would be identical to the base one. What am I doing wrong? Sorry if that wasn't clear. The behaviour of Start() and Stop() in the derived class is different (it's a different machine) - but I want to use the original base

How can C++ virtual functions be implemented except vtable? [duplicate]

丶灬走出姿态 提交于 2019-11-28 08:25:50
Possible Duplicate: A question about virtual mechanism in C++ Is using vtable the only way to implement virtual member functions mechanism in C++? What other ways exist? Another known mechanism is type dispatch functions. Effectively, you replace the vtable pointer by a typeid (small enum). The (dyanmic) linker collects all overrides of a given virtual function, and wraps them in one big switch statement on the typeid field. The theoretical justifcation is that this replaces an indirect jump (non-predicatble) by lots of predicatable jumps. With some smarts in choosing enum values, the switch

[C++]Covariant return types

假如想象 提交于 2019-11-28 08:19:32
问题 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 *':

override on non-virtual functions

旧时模样 提交于 2019-11-28 08:04:56
The C++11 FDIS it says If a virtual function is marked with the virt-specifier override and does not override a member function of a base class, the program is ill-formed. [ Example: struct B { virtual void f(int); }; struct D : B { void f(long) override; // error: wrong signature overriding B::f void f(int) override; // OK }; What if B::f would not have been marked virtual? Is the program ill-formed, then? Or is override then to be ignored`. I can not find any handling of this case in the std text. Update 1/2 (merged) I forwarded a request to the C++ Editors to look into things. Thanks