virtual-functions

Inversion of generated vtable functions order for functions with the same name

本小妞迷上赌 提交于 2021-02-11 13:56:38
问题 If, using Visual Studio 2019, I compile this C++ code with two virtual methods having the same name but different arguments: struct MyStruct { virtual void foo(float) = 0; virtual void foo(int) = 0; }; class MyClass : public MyStruct { public: void foo(float) {} void foo(int) {} }; static MyClass c; The order of methods in the generated class' vtable is inverted. Here is the output in https://godbolt.org const MyClass::`vftable' DQ FLAT:const MyClass::`RTTI Complete Object Locator' ; MyClass:

Can multiple base classes have the same virtual method?

与世无争的帅哥 提交于 2021-02-10 18:50:53
问题 class A has a pure virtual method read() class B has an implemented virtual method read() I have a class C that inherits A and B Can this happen? What I'm trying to achieve is that the two base classes A and B complement each other. So C read() method would actually call class B read() class A { virtual int get_data() = 0; void print() { log(get_data()); } } class B { virtual int get_data() { return 4; } } class C : public A, public B { } C my_class; my_class.print(); // should log 4; I'm not

Can multiple base classes have the same virtual method?

。_饼干妹妹 提交于 2021-02-10 18:50:34
问题 class A has a pure virtual method read() class B has an implemented virtual method read() I have a class C that inherits A and B Can this happen? What I'm trying to achieve is that the two base classes A and B complement each other. So C read() method would actually call class B read() class A { virtual int get_data() = 0; void print() { log(get_data()); } } class B { virtual int get_data() { return 4; } } class C : public A, public B { } C my_class; my_class.print(); // should log 4; I'm not

“Direct” vs “virtual” call to a virtual function

旧巷老猫 提交于 2021-02-07 00:01:05
问题 I am self-taught, and therefore am not familiar with a lot of terminology. I cannot seem to find the answer to this by googling: What is a "virtual" vs a "direct" call to a virtual function? This pertains to terminology, not technicality. I am asking for when a call is defined as being made "directly" vs "virtually". It does not pertain to vtables, or anything else that has to do with the implementation of these concepts. 回答1: The answer to your question is different at different conceptual

“Direct” vs “virtual” call to a virtual function

删除回忆录丶 提交于 2021-02-06 23:48:18
问题 I am self-taught, and therefore am not familiar with a lot of terminology. I cannot seem to find the answer to this by googling: What is a "virtual" vs a "direct" call to a virtual function? This pertains to terminology, not technicality. I am asking for when a call is defined as being made "directly" vs "virtually". It does not pertain to vtables, or anything else that has to do with the implementation of these concepts. 回答1: The answer to your question is different at different conceptual

“Direct” vs “virtual” call to a virtual function

自古美人都是妖i 提交于 2021-02-06 23:40:22
问题 I am self-taught, and therefore am not familiar with a lot of terminology. I cannot seem to find the answer to this by googling: What is a "virtual" vs a "direct" call to a virtual function? This pertains to terminology, not technicality. I am asking for when a call is defined as being made "directly" vs "virtually". It does not pertain to vtables, or anything else that has to do with the implementation of these concepts. 回答1: The answer to your question is different at different conceptual

How to make virtual functions of a super class overrideable for grandchildren in C++? [duplicate]

孤街浪徒 提交于 2021-02-05 06:37:32
问题 This question already has answers here : What is object slicing? (18 answers) Closed 3 years ago . Hey guys here is some code I am going to run, issue is it doesn't work the way I intend to. I am unable to figure out what's wrong with it. I am c++ noob please help. #include <iostream> #include <cmath> #include <stdexcept> using namespace std; /** * Super class */ class Shape { protected: int _dimensions; public: Shape() : _dimensions{0} {}; Shape(int dims) : _dimensions{dims} {}; virtual

How to make virtual functions of a super class overrideable for grandchildren in C++? [duplicate]

寵の児 提交于 2021-02-05 06:37:08
问题 This question already has answers here : What is object slicing? (18 answers) Closed 3 years ago . Hey guys here is some code I am going to run, issue is it doesn't work the way I intend to. I am unable to figure out what's wrong with it. I am c++ noob please help. #include <iostream> #include <cmath> #include <stdexcept> using namespace std; /** * Super class */ class Shape { protected: int _dimensions; public: Shape() : _dimensions{0} {}; Shape(int dims) : _dimensions{dims} {}; virtual

How to implement device side CUDA virtual functions?

杀马特。学长 韩版系。学妹 提交于 2021-02-02 08:16:00
问题 I see that CUDA doesn't allow for classes with virtual functions to be passed into kernel functions. Are there any work-arounds to this limitation? I would really like to be able to use polymorphism within a kernel function. Thanks! 回答1: The most important part of Robert Crovella's comment is: The objects simply need to be created on the device. So keeping that in mind, I was dealing with situation where I had an abstract class Function and then some implementations of it encapsulating

How to implement device side CUDA virtual functions?

淺唱寂寞╮ 提交于 2021-02-02 08:13:54
问题 I see that CUDA doesn't allow for classes with virtual functions to be passed into kernel functions. Are there any work-arounds to this limitation? I would really like to be able to use polymorphism within a kernel function. Thanks! 回答1: The most important part of Robert Crovella's comment is: The objects simply need to be created on the device. So keeping that in mind, I was dealing with situation where I had an abstract class Function and then some implementations of it encapsulating