virtual-functions

If a private virtual function is overridden as a public function in the derived class, what are the problems?

拈花ヽ惹草 提交于 2019-12-28 18:51:12
问题 using namespace std; #include <cstdio> #include <iostream> class One{ private: virtual void func(){ cout<<"bark!"<<endl; } }; class Two: public One{ public: void func(){ cout<<"two!"<<endl; } }; int main(){ One *o = new Two(); o->func(); } Why is there an error on o->func() ? I don't know the mechanism behind it... In my opinion, o->func() should call the func() in the derived class, which is public, so there wouldn't be problems, but it says: error: ‘virtual void One::func()’ is private 回答1:

list of polymorphic objects

帅比萌擦擦* 提交于 2019-12-28 18:44:21
问题 I have a particular scenario below. The code below should print 'say()' function of B and C class and print 'B says..' and 'C says...' but it doesn't .Any ideas.. I am learning polymorphism so also have commented few questions related to it on the lines of code below. class A { public: // A() {} virtual void say() { std::cout << "Said IT ! " << std::endl; } virtual ~A(); //why virtual destructor ? }; void methodCall() // does it matters if the inherited class from A is in this method { class

Pure virtual call in destructor of most derived class

允我心安 提交于 2019-12-25 04:15:34
问题 I know you shouldn't call any virtual function in the ctor or dtor of the base class, but what about from that of the most derived class? Should be fine right? E.g. class base { ... virtual void free() = 0; }; class child : public base { ... free() {/* free memory */} ~child() {free();} }; 回答1: Well, you can do it, but the dynamic type of *this inside child::~child() is child , and not anything more derived. So when you have a further derived class class foo : child which overrides free() ,

inline virtual method in template class

对着背影说爱祢 提交于 2019-12-25 01:59:46
问题 I have a template base class with a get_p_pow method that is called by a foo function: template <typename T_container> class base { public: int foo() { ... get_p_pow(p_pow, delta_p); ... } ... protected: virtual T_container& get_p_pow(T_container &p_pow, double delta_p) const { p_pow(0) = 1.0; p_pow(1) = delta_p; for (difference_type i = 2; i <= order; ++i) { p_pow(i) *= p_pow(i-1)*delta_p; } return p_pow; } int order; }; For some derived classes, the value of order is set to a specific

Liskov Substitution Principle with multiple inheritance heirachies

拈花ヽ惹草 提交于 2019-12-25 01:30:43
问题 I am trying to come up with an object oriented design, and having difficulty satisfying the Liskov Substitution Principle. Here is an illustrative example: class Food { public: virtual void printName() { //...... } }; class Fruit : public Food { }; class Meat : public Food { }; class Animal { public: Food *_preferredFood; virtual void setFoodPreference(Food *food)=0; }; class Carnivore: public Animal { public: void setFoodPreference(Food *food) { this->_preferredFood = dynamic_cast<Meat *>

Dynamic binding in C++ on copied object

℡╲_俬逩灬. 提交于 2019-12-25 01:17:05
问题 I have a problem in virtual function: Here is some code as an example: class A { public : virtual void print(void) { cout<< "A::print()"<<endl; } }; class B : public A { public : virtual void print(void) { cout<<"B::print()"<<endl; } }; class C : public A { public : void print(void) { cout<<"C::print()"<<endl; } }; int main(void) { A a,*pa,*pb,*pc; B b; C c; pa=&a; pb=&b; pc=&c; pa->print(); pb->print(); pc->print(); a=b; a.print(); return 0; } the result: A::print() B::print() C::print() A:

C++ inherit template class

余生颓废 提交于 2019-12-25 00:34:48
问题 I've got a peculiar request, hopefully it's not too far fetched and can be done. I've got a template class template<class T> class Packable { public: // Packs a <class T> into a Packet (Packet << T) virtual sf::Packet& operator <<(sf::Packet& p) const = 0; friend sf::Packet& operator <<(sf::Packet& p, const T &t); friend sf::Packet& operator <<(sf::Packet& p, const T *t); // Packs a Packet into a <class T> (T << Packet) virtual T operator <<(sf::Packet& p) const = 0; friend T& operator <<(T

Virtual function invocation from constructor

这一生的挚爱 提交于 2019-12-24 04:40:54
问题 Maybe I am wrong, but this seems to be a very basic question. Suddenly my inheritance chain stopped working. Writing a small basic test application proved that it was me that was wrong (so I can't blame the compiler). I have a base class, with the default behavior in a virtual function. A child class derives from that and changes the behavior. #include <iostream> class Base { public: Base() { print(); } ~Base() {} protected: virtual void print() { std::cout << "base\n"; } }; class Child :

C# virtual methods question

。_饼干妹妹 提交于 2019-12-24 03:05:10
问题 There is a thing I do not understand well: when virtual method is called, the base method is called as well? Because when I use public override WinForm OnPaint method, in its body base.OnPaint(e) is called. I do not understand it, I thought virtual methods overrides the original one. If it is not usually called, why it is called in this case? Thank you 回答1: when virtual method is called, the base method is called as well? No. Because when I use public override OnPaint(), in its body base

Define the method once to be virtual in the inheritance hierarchy to make polymorphism to work

限于喜欢 提交于 2019-12-24 01:25:45
问题 Is it sufficient to define the method once to be virtual in the inheritance hierarchy to make polymorphism to work. In the following example Der::f is not defined to be virtual but d2->f(); prints der2 I am using VS IDE (may be it is only there...) class Base { public: virtual void f() { std::cout << "base"; } }; class Der : public Base { public: void f() { std::cout << "der"; } //should be virtual? }; class Der2 : public Der { public: void f() { std::cout << "der2"; } }; int main() { Der* d2