virtual

What does 'has virtual method … but non-virtual destructor' warning mean during C++ compilation?

泪湿孤枕 提交于 2019-11-27 10:45:18
问题 #include <iostream> using namespace std; class CPolygon { protected: int width, height; public: virtual int area () { return (0); } }; class CRectangle: public CPolygon { public: int area () { return (width * height); } }; Has compilation warning Class '[C@1a9e0f7' has virtual method 'area' but non-virtual destructor How to understand this warning and how to improve the code? [EDIT] is this version correct now? (Trying to give answer to elucidate myself with the concept) #include <iostream>

How to design a C++ API for binary compatible extensibility

蓝咒 提交于 2019-11-27 09:38:24
问题 I am designing an API for a C++ library which will be distributed in a dll / shared object. The library contains polymorhic classes with virtual functions. I am concerned that if I expose these virtual functions on the DLL API, I cut myself from the possibility of extending the same classes with more virtual functions without breaking binary compatibility with applications built for the previous version of the library. One option would be to use the PImpl idiom to hide all the classes having

Why does a virtual function get hidden?

…衆ロ難τιáo~ 提交于 2019-11-27 09:01:54
I have the following classes: class A { public: virtual void f() {} }; class B : public A{ public: void f(int x) {} }; If I say B *b = new B(); b->f(); the compiler says error C2660: 'B::f' : function does not take 0 arguments. Shouldn't the function in B overload it, since it is a virtual function? Do virtual functions get hidden like this? EDIT : I indeed meant to inherit B from A, which shows the same behaviour. Assuming you intended B to derive from A : f(int) and f() are different signatures, hence different functions. You can override a virtual function with a function that has a

C++多态有哪几种方式?

て烟熏妆下的殇ゞ 提交于 2019-11-27 08:46:30
C++多态方式: (1)静态多态(重载,模板) 是在 编译的时候 ,就确定调用函数的类型。 (2)动态多态(覆盖,虚函数实现) 在 运行的时候 ,才确定调用的是哪个函数,动态绑定。运行基类指针指向派生类的对象,并调用派生类的函数。 虚函数实现原理:虚函数表和虚函数指针。 纯虚函数: virtual int fun() = 0; 多态基础介绍: =============================================================================================== 首先,什么是多态(Polymorphisn)?按字面的意思就是"多种形状"。我手头的书上没有找到一个多态的理论性的概念的描述。 暂且引用一下 Charlie Calverts的对多态的描述吧——多态性是允许你将父对象设置成为和一个或更多的他的子对象相等的技术,赋值之后,父对象就可以根据当前赋值给它的子对象的特性以不同的方式运作(摘自"Delphi4 编程技术内幕")。 简单的说,就是一句话:允许将子类类型的指针赋值给父类类型的指针。多态性在Object Pascal和C++中都是通过虚函数(Virtual Function) 实现的。 好,接着是"虚函数"(或者是"虚方法")。虚函数就是允许被其子类重新定义的成员函数。而子类重新定义父类虚函数的做法,称为

Print address of virtual member function

主宰稳场 提交于 2019-11-27 08:34:32
I am trying to print the address of a virtual member function. If I know which class implements the function I can write: print("address: %p", &A::func); But I want to do something like this: A *b = new B(); printf("address: %p", &b->func); printf("address: %p", &b->A::func); However this does not compile. Is it possible to do something like this, perhaps looking up the address in the vtable at runtime? Lukas W Currently there is no standard way of doing this in C++ although the information must be available somewhere. Otherwise, how could the program call the function? However, GCC provides

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

时间秒杀一切 提交于 2019-11-27 07:54:13
问题 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(); } 回答1: Yes, it will use the vtable (only non-virtual

Array of polymorphic base class objects initialized with child class objects

醉酒当歌 提交于 2019-11-27 07:49:14
问题 Sorry for the complicated title. I have something like this: class Base { public: int SomeMember; Base() : SomeMember(42) {} virtual int Get() { return SomeMember; } }; class ChildA : public Base { public: virtual int Get() { return SomeMember*2; } }; class ChildB : public Base { public: virtual int Get() { return SomeMember/2; } }; class ChildC : public Base { public: virtual int Get() { return SomeMember+2; } }; Base ar[] = { ChildA(), ChildB(), ChildC() }; for (int i=0; i<sizeof(ar)/sizeof

Virtual DOM(八)

ε祈祈猫儿з 提交于 2019-11-27 07:48:27
Virtual DOM 这个概念相信大部分人都不会陌生,它产生的前提是浏览器中的 DOM 是很“昂贵"的,为了更直观的感受,我们可以简单的把一个简单的 div 元素的属性都打印出来,如图所示: 可以看到,真正的 DOM 元素是非常庞大的,因为浏览器的标准就把 DOM 设计的非常复杂。当我们频繁的去做 DOM 更新,会产生一定的性能问题。 而 Virtual DOM 就是用一个原生的 JS 对象去描述一个 DOM 节点,所以它比创建一个 DOM 的代价要小很多。在 Vue.js 中,Virtual DOM 是用 VNode 这么一个 Class 去描述,它是定义在 src/core/vdom/vnode.js 中的。 export default class VNode { tag: string | void; data: VNodeData | void; children: ?Array<VNode>; text: string | void; elm: Node | void; ns: string | void; context: Component | void; // rendered in this component's scope key: string | number | void; componentOptions:

Why does this code crash at the places mentioned?

亡梦爱人 提交于 2019-11-27 07:04:19
问题 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]

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

南楼画角 提交于 2019-11-27 06:58:25
问题 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