virtual-functions

Use-cases of pure virtual functions with body?

我是研究僧i 提交于 2019-12-17 08:24:07
问题 I recently came to know that in C++ pure virtual functions can optionally have a body. What are the real-world use cases for such functions? 回答1: The classic is a pure virtual destructor: class abstract { public: virtual ~abstract() = 0; }; abstract::~abstract() {} You make it pure because there's nothing else to make so, and you want the class to be abstract, but you have to provide an implementation nevertheless, because the derived classes' destructors call yours explicitly. Yeah, I know,

Practical usage of virtual functions in c#

爷,独闯天下 提交于 2019-12-17 08:18:13
问题 What 's the practical usage of virtual functions in c#? 回答1: So basically if in your ancestor class you want a certain behaviour for a method. If your descendent uses the same method but has a different implementation you can override it, If it has a virtual keyword. using System; class TestClass { public class Dimensions { public const double pi = Math.PI; protected double x, y; public Dimensions() { } public Dimensions (double x, double y) { this.x = x; this.y = y; } public virtual double

C++ object size with virtual methods

不打扰是莪最后的温柔 提交于 2019-12-17 04:31:07
问题 I have some questions about the object size with virtual. 1) virtual function class A { public: int a; virtual void v(); } The size of class A is 8bytes....one integer(4 bytes) plus one virtual pointer(4 bytes) It's clear! class B: public A{ public: int b; virtual void w(); } What's the size of class B? I tested using sizeof B, it prints 12 Does it mean that only one vptr is there even both of class B and class A have virtual function? Why there is only one vptr? class A { public: int a;

C++ object size with virtual methods

心不动则不痛 提交于 2019-12-17 04:30:11
问题 I have some questions about the object size with virtual. 1) virtual function class A { public: int a; virtual void v(); } The size of class A is 8bytes....one integer(4 bytes) plus one virtual pointer(4 bytes) It's clear! class B: public A{ public: int b; virtual void w(); } What's the size of class B? I tested using sizeof B, it prints 12 Does it mean that only one vptr is there even both of class B and class A have virtual function? Why there is only one vptr? class A { public: int a;

C++ object size with virtual methods

北战南征 提交于 2019-12-17 04:30:02
问题 I have some questions about the object size with virtual. 1) virtual function class A { public: int a; virtual void v(); } The size of class A is 8bytes....one integer(4 bytes) plus one virtual pointer(4 bytes) It's clear! class B: public A{ public: int b; virtual void w(); } What's the size of class B? I tested using sizeof B, it prints 12 Does it mean that only one vptr is there even both of class B and class A have virtual function? Why there is only one vptr? class A { public: int a;

Overriding public virtual functions with private functions in C++

梦想的初衷 提交于 2019-12-17 04:23:28
问题 Is there is any reason to make the permissions on an overridden C++ virtual function different from the base class? Is there any danger in doing so? For example: class base { public: virtual int foo(double) = 0; } class child : public base { private: virtual int foo(double); } The C++ faq says that it is a bad idea, but doesn't say why. I have seen this idiom in some code and I believe that the author was attempting to make the class final, based on an assumption that it is not possible to

Overriding public virtual functions with private functions in C++

五迷三道 提交于 2019-12-17 04:23:15
问题 Is there is any reason to make the permissions on an overridden C++ virtual function different from the base class? Is there any danger in doing so? For example: class base { public: virtual int foo(double) = 0; } class child : public base { private: virtual int foo(double); } The C++ faq says that it is a bad idea, but doesn't say why. I have seen this idiom in some code and I believe that the author was attempting to make the class final, based on an assumption that it is not possible to

What is the difference between an abstract function and a virtual function?

送分小仙女□ 提交于 2019-12-17 02:02:11
问题 What is the difference between an abstract function and a virtual function? In which cases is it recommended to use virtual or abstract? Which one is the best approach? 回答1: An abstract function cannot have functionality. You're basically saying, any child class MUST give their own version of this method, however it's too general to even try to implement in the parent class. A virtual function , is basically saying look, here's the functionality that may or may not be good enough for the

C++ “virtual” keyword for functions in derived classes. Is it necessary?

冷暖自知 提交于 2019-12-16 20:14:47
问题 With the struct definition given below... struct A { virtual void hello() = 0; }; Approach #1: struct B : public A { virtual void hello() { ... } }; Approach #2: struct B : public A { void hello() { ... } }; Is there any difference between these two ways to override the hello function? 回答1: They are exactly the same. There is no difference between them other than that the first approach requires more typing and is potentially clearer. 回答2: The 'virtualness' of a function is propagated

Polymorphism Without Virtual Functions

大兔子大兔子 提交于 2019-12-13 23:08:29
问题 I am trying to optimize the run time of my code and I was told that removing unnecessary virtual functions was the way to go. With that in mind I would still like to use inheritance to avoid unnecessary code bloat. I thought that if I simply redefined the functions I wanted and initialized different variable values I could get by with just downcasting to my derived class whenever I needed derived class specific behavior. So I need a variable that identifies the type of class that I am dealing