virtual-functions

Can virtual functions be constexpr?

核能气质少年 提交于 2019-12-03 10:16:20
Can virtual functions like X::f() in the following code struct X { constexpr virtual int f() const { return 0; } }; be constexpr ? Kerrek SB This answer is no longer correct as of C++20. No. From [dcl.constexpr]/3 (7.1.5, "The constexpr specifier"): The definition of a constexpr function shall satisfy the following requirements: — it shall not be virtual Up through C++17, virtual functions could not be declared constexpr . The general reason being that, in constexpr code, everything happen can at compile time. So there really isn't much point to having a function which takes a reference to a

When do we break binary compatibility

丶灬走出姿态 提交于 2019-12-03 09:51:40
问题 I was under the impression that whenever you do one of these: Add a new public virtual method virtual void aMethod(); Add a new public non-virtual method void aMethod(); Implement a public pure-virtual method from an interface virtual void aMethod override; Was actually breaking binary compatibility, meaning that if a project had build on a previous version of the DLL, it would not be able to load it now that there is new methods available. From what I have tested using Visual Studio 2012,

Correct Implementation of Virtual Functions in PHP?

可紊 提交于 2019-12-03 09:44:41
at my working place (php only) we have a base class for database abstraction. When you want to add a new database table to the base layer, you have to create a subclass of this base class and override some methods to define individual behaviour for using this table. The normal behaviour should stay the same. Now I have seen many new programmers at our company, who just override the method for the default behaviour. Some are so "nice" to put in all the default behaviour and just add there individual stuff where they like it, others kill themself trying to use the baseclass and their inheritor.

What is the use of Java virtual method invocation? [closed]

 ̄綄美尐妖づ 提交于 2019-12-03 06:53:24
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 7 years ago . I understand what is java method invocation and have practiced many examples using it. I want to know what is the practical situation or need of this concept. It would be of great help if anyone could give a real

Arduino C++ code: can you use virtual functions and exceptions?

五迷三道 提交于 2019-12-03 05:57:17
问题 Following up on this comment from the question Writing firmware: assembly or high level?: When compiling C++ code for the Arduino platform, can you use virtual functions, exceptions, etc? Or would you want to (have to) use a subset of C++ (as described in the comment)? Any other caveats when programming for the Arduino platform? 回答1: The Arduino environment uses the AVR version of the GCC toolchain. The code is compiled as C++, so you can use classes. Virtual functions are possible; the

Can you cache a virtual function lookup in C++?

若如初见. 提交于 2019-12-03 04:08:20
问题 Say I have a virtual function call foo() on an abstract base class pointer, mypointer->foo(). When my app starts up, based on the contents of a file, it chooses to instantiate a particular concrete class and assigns mypointer to that instance. For the rest of the app's life, mypointer will always point to objects of that concrete type. I have no way to know what this concrete type is (it may be instantiated by a factory in a dynamically loaded library). I only know that the type will stay the

why do we actually have virtual functions?

限于喜欢 提交于 2019-12-03 02:39:11
I am new to C++. Could anybody tell me the difference between method overriding and virtual function concepts in c++. The functionality of virtual functions can be over-ridden in its derived classes. Redefining a function in a derived class is called function overriding. why do we actually have virtual functions? pyon ABSTRACT In this paper, we discuss virtual functions in C++. Part zero explains how virtual functions are declared and overridden. Part one attempts (and perhaps fails) to explain how virtual functions are implemented. Part two is a sample program that uses the example classes

When does the vptr (pointing to vtable) get initialized for a polymorphic class?

拈花ヽ惹草 提交于 2019-12-03 00:29:41
This is not about "When VTABLE is created?" . Rather, when the VPTR should be initialized? Is it at the beginning/end of the constructor or before/after the constructor? A::A () : i(0), j(0) -->> here ? { -->> here ? //... -->> here ? } The machinery for virtual calls (usually a v-table, but doesn't need to be) is set up during the ctor-initializer , after construction of base subobjects and before construction of members. Section [class.base.init] decrees: Member functions (including virtual member functions, 10.3) can be called for an object under construction. Similarly, an object under

When do we break binary compatibility

空扰寡人 提交于 2019-12-03 00:26:29
I was under the impression that whenever you do one of these: Add a new public virtual method virtual void aMethod(); Add a new public non-virtual method void aMethod(); Implement a public pure-virtual method from an interface virtual void aMethod override; Was actually breaking binary compatibility, meaning that if a project had build on a previous version of the DLL, it would not be able to load it now that there is new methods available. From what I have tested using Visual Studio 2012, none of these break anything. Dependency Walker reports no error and my test application was calling the

method in the derived class got executed without a *virtual* keyword in the referred class

江枫思渺然 提交于 2019-12-02 21:22:34
问题 class vehicle { public: virtual void drive() { cout<<"in vehicle drive"<<endl; } }; class bus:public vehicle { public: void drive() { cout<<"in bus drive"<<endl; } }; class supervehicle:public bus { public: void drive() { cout<<"in supervehicle"<<endl; } }; int main() { bus *b; b=new supervehicle(); b->drive(); return 0; } I expected the ouput as "in bus drive" , but the output is "in supervehicle" . If the virtual keyword is associated with the drive method in the bus class then for sure the