virtual

Hoisting the dynamic type out of a loop (a.k.a. doing Java the C++ way)

孤者浪人 提交于 2019-11-27 22:54:41
I was discussing the merits of "modern" languages compared to C++ with some friends recently, when the following came up (I think inspired by Java): Does any C++ compiler optimize dynamic dispatch out of a loop? If not, is there any kind of construction that would allow the author to force (or strongly encourage) such an optimization? Here's the example. Suppose I have a polymorphic hierarchy: struct A { virtual int f() { return 0; } }; struct B : A { virtual int f() { return /* something complicated */; } /*...*/ }; Now I have a loop that accumulates f() : int acc(const A * p, unsigned int N)

Can't get Mongoose virtuals to be part of the result object

久未见 提交于 2019-11-27 22:44:33
bI'm declaring a virtual that I want to appear as part of the results of its schema's queries, but it's not showing up when I do a console.log on the object. Here's the schema: var schema = new mongoose.Schema( { Name: { type: String } }, { toObject: { virtuals: true } }); schema.virtual("Greet").get(function() { return "My name is " + this.Name; }); Should that toObject not set the virtual as a property of the results of any queries? It does not, nor does schema.set("toObject", { virtuals: true }). Am I doing this right? Because you're using JSON.stringify in your console.log call, that

继承与虚函数

馋奶兔 提交于 2019-11-27 22:19:43
  使用继承可以定义相似的类型并对其相似关系建模。 1 class Quote { 2 public: 3 std::string isbn() const; 4 virtual double net_price(std::size_t n) const; 5 }; 6 7 class Bulk : public Quote { 8 public: 9 double net_price(std::size_t n) const override; 10 }; View Code    虚函数: 通过virtual生命基类中希望派生类定义适合自身的版本,则用virtual来声明基类的函数,在派生类中对应函数的形参列表后面添加override关键字。   动态绑定:使用同一段代码,对具有一定区别的相似类型以统一的方式进行处理。 double print_total(ostream& os, const Quote& item, size_t n) { double ret = item.net_price(n); os << "ISBN: " << item.isbn() << " # sold: " << n << "total due: " << ret << endl; return ret; } View Code   调用方法: 1 print_total(cout,

Template definitions outside class body

谁说胖子不能爱 提交于 2019-11-27 22:13:59
问题 Is it O.K. to define virtual function of class template outside its body? Virtual function can not be inlined, but to avoid multiple definitions in compilation units they shall be marked inline (assuming that template headers will be included in multiple source files). On the other hand compiler is free to ignore inline , so this seems valid. By an example, is the code below correct: template <typename T> class C { public: virtual void f(T val); }; template <typename T> inline void C<T>::f(T

定义基类和派生类

自闭症网瘾萝莉.ら 提交于 2019-11-27 21:53:29
成员函数与继承   在C++语言中,基类必须将它的两种成员函数区分开来:一种是基类希望派生类进行覆盖的函数;另一类是基类希望派生类直接继承而不要改变的函数。对于前者,基类通常将其定义成虚函数(virtual)。当我们使用指针或引用调用虚函数时,该调用将被动态绑定。根据引用或指针所绑定的对象类型不同,该调用可能执行基类版本,也可能执行某个派生类版本。   基类通过在其成员函数的声明语句之前加上关键字virtual使得该函数执行动态绑定。 任何构造函数之外的非静态函数都可以是虚函数。 关键字virtual只能出现在类内部的声明语句而不能用于类外部的函数定义。 如果基类把一个函数声明成虚函数,则该函数在派生类中隐式也是虚函数。   成员函数如果没有被声明为虚函数,则其解析过程发生在编译时而非运行时。 访问控制与继承 来源: https://www.cnblogs.com/bootblack/p/11379293.html

C++_练习—多态_virtual

梦想与她 提交于 2019-11-27 21:39:43
多态_virtual /* 父类指针定义对象,当基类与派生类存在相同成员函数,编译器设置为基类中的版本, 为静态多态/静态链接/早绑定:函数调用在程序执行前就准备好了! 在父类相同函数前加 virtual 则编译器看指针的内容,而不是类型! 子类定义对象,当基类与派生类存在相同成员函数,调用子类! 父类定义对象,当基类与派生类存在相同成员函数,调用父类! */ 1 /* 父类指针定义对象,当基类与派生类存在相同成员函数,编译器设置为基类中的版本, 2 为静态多态/静态链接/早绑定:函数调用在程序执行前就准备好了! 3 在父类相同函数前加 virtual 则编译器看指针的内容,而不是类型! 4 5 子类定义对象,当基类与派生类存在相同成员函数,调用子类!    父类定义对象,当基类与派生类存在相同成员函数,调用父类! 6 */ 7 8 #include <iostream> 9 10 using namespace std; 11 12 class parent { 13 public: 14 parent(int age) { 15 this->age = age; 16 } 17 18 void pri(void) { 19 cout << "parent = " << age << endl; 20 } 21 22 private: 23 int age; 24 }; 25

When to mark a function in C++ as a virtual?

僤鯓⒐⒋嵵緔 提交于 2019-11-27 21:29:45
Because of C++ nature of static-binding for methods, this affects the polymorphic calls. From Wikipedia: Although the overhead involved in this dispatch mechanism is low, it may still be significant for some application areas that the language was designed to target. For this reason, Bjarne Stroustrup, the designer of C++, elected to make dynamic dispatch optional and non-default. Only functions declared with the virtual keyword will be dispatched based on the runtime type of the object; other functions will be dispatched based on the object's static type. So the code: Polygon* p = new

C++ virtual function table memory cost

天大地大妈咪最大 提交于 2019-11-27 21:27:37
Consider: class A { public: virtual void update() = 0; } class B : public A { public: void update() { /* stuff goes in here... */ } private: double a, b, c; } class C { // Same kind of thing as B, but with different update function/data members } I'm now doing: A * array = new A[1000]; array[0] = new B(); array[1] = new C(); //etc., etc. If i call sizeof(B) , the size returned is the size required by the 3 double members, plus some overhead required for the virtual function pointer table. Now, back to my code, it turns out that 'sizeof(myclass)' is 32; that is, I am using 24 bytes for my data

In C++, is a function automatically virtual if it overrides a virtual function?

自古美人都是妖i 提交于 2019-11-27 21:15:10
I would expect that if foo is declared in class D , but not marked virtual, then the following code would call the implementation of foo in D (regardless of the dynamic type of d ). D& d = ...; d.foo(); However, in the following program, that is not the case. Can anyone explain this? Is a method automatically virtual if it overrides a virtual function? #include <iostream> using namespace std; class C { public: virtual void foo() { cout << "C" << endl; } }; class D : public C { public: void foo() { cout << "D" << endl; } }; class E : public D { public: void foo() { cout << "E" << endl; } }; int

C++ Virtual function being hidden

非 Y 不嫁゛ 提交于 2019-11-27 20:09:50
I'm having a problem with C++ inheritance. I have a class hierarchy: class A { public: virtual void onFoo() {} virtual void onFoo(int i) {} }; class B : public A { public: virtual void onFoo(int i) {} }; class C : public B { }; int main() { C* c = new C(); c->onFoo(); //Compile error - doesn't exist } My question is: why doesn't this compile? My understanding is that C should inherit both onFoo functions from A - and in fact, this compiles if you remove the redefinition of onFoo in B - but g++ gives an error that C has no onFoo() function. The issue that you are experiencing is related to how