virtual-functions

in c++ when subclassing why sometimes need to add virtual keyword to overridden function?

∥☆過路亽.° 提交于 2019-12-04 22:21:47
Why do I sometimes see in C++ examples when talking about subclassing / inheritance, the base class has virtual keyword and sometimes the overridden function has also the virtual keyword, why it's necessary to add to the subclass the virtual key word sometimes? For example: class Base { Base(){}; virtual void f() ...... } }; class Sub : public Base { Sub(){}; virtual void f() ...new impl of f() ... } }; It is not necessary, but it helps readability if you only see the derived class definition. §10.3 [class.virtual]/3 If a virtual member function vf is declared in a class Base and in a class

Can virtual functions be constexpr?

泪湿孤枕 提交于 2019-12-04 16:29:56
问题 Can virtual functions like X::f() in the following code struct X { constexpr virtual int f() const { return 0; } }; be constexpr ? 回答1: 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 回答2: Up through C++17, virtual functions could not be declared constexpr . The general reason being that, in constexpr code, everything happen

Correct Implementation of Virtual Functions in PHP?

岁酱吖の 提交于 2019-12-04 15:37:25
问题 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

Virtual Functions in C++ and Java

时光怂恿深爱的人放手 提交于 2019-12-04 12:50:44
I have been reading about Virtual functions and found, VF are used in polymorphism of an inherited class. So , if a class and derived class both have the same function name, VF binds the appropriate function to the function call. i.e. If the function in question is designated virtual in the base class then the derived class's function would be called. If it is not virtual, the base class's function would be called. In Java by default: all functions are Virtual C++: Non-virtual and can be made Virtual in Java by using final, private access modifier while in C++ using Virtual keyword to make a

A constructor cannot be virtual

久未见 提交于 2019-12-04 12:24:53
问题 In one of the C++ tutorials in internet, i found out the below description on why a constructor cannot be virtual We cannot declare a virtual constructor. We should specify the exact type of the object at compile time, so that the compiler can allocate memory for that specific type. Is this description correct ? I am getting confused particularly with the phrase: so that the compiler can allocate memory for that specific type . 回答1: As Bjarne himself explains here A virtual call is a

why do we actually have virtual functions?

南楼画角 提交于 2019-12-04 09:19:39
问题 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? 回答1: 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

Why can't the virtual function table pointer (vfptr) be static in C++?

跟風遠走 提交于 2019-12-04 08:11:00
If the virtual function table is the same for all objects of the class, then why can't the pointer to that table (vfptr) be static and be shared across all the objects? The vtable is essentially static. But you need a vptr member actually inside the object to do virtual dispatch and other RTTI operations. On a vptr implementation, this C++ code: class Base { public: virtual void f(); }; class Derived : public Base { public: virtual void f(); }; may act similarly to something like this: class Base { public: Base::Base() : m_vptr(&Base_vtab) {} Base::Base(const Base& b) : m_vptr(&Base_vtab) {}

C++ Base constructor calling with parameter that will be constructed in the derived constructor

穿精又带淫゛_ 提交于 2019-12-04 07:53:29
QUESTION 1) class Base { Base(std::string name); virtual std::string generateName(); } class Derived : Base { Derived(); virtual std::string generateName(); } here comes the question : what method will be called on generateName() ? Derived :: Derived : Base(generateName()) { //what method will be called on generateName() ? } QUESTION 2) how should i make it? if the default constructor must accept a parameter, but i need to generate that parameter in the Derived constructor? First, the solution: use a static member function or a nonmember function. As for the behavior, Derived::generateName()

C++ virtual function not called in subclass

二次信任 提交于 2019-12-04 04:21:54
Consider this simple situation: A.h class A { public: virtual void a() = 0; }; B.h #include <iostream> class B { public: virtual void b() {std::cout << "b()." << std::endl;}; }; C.h #include "A.h" #include "B.h" class C : public B, public A { public: void a() {std::cout << "a() in C." << std::endl;}; }; int main() { B* b = new C(); ((A*) b)->a(); // Output: b(). A* a = new C(); a->a(); // Output:: a() in C. return 0; } In other words: - A is a pure virtual class. - B is a class with no super class and one non-pure virtual function. - C is a subclass of A and B and overrides A's pure virtual

Cuda virtual class

烂漫一生 提交于 2019-12-04 03:38:40
问题 I would like to execute some virtual methods in a cuda kernel, but instead of creating the object in the same kernel I would like to create it on the host and copy it to gpu memory. I am successfully creating objects in a kernel and call a virtual method. The problem arises when copying the object. This makes sense because obviously the virtual function pointer is bogus. What happens is simply "Cuda grid launch failed", at least this is what Nsight says. But when having a look at the SASS it