Category : polymorphism

I have two vectors of different classes, where Slot,Field, and Vacuum are subclasses of Space. vector<Space *> av_spaces; vector<Slot> av_slots; vector<Field> av_fields; vector<Vacuum> av_vacuums; Here’s what I am trying to do. av_slots.push_back(av_spaces[i]) Now my program validates that av_spaces[i] is a Slot for sure, before trying to push it. This is the error I am getting, ..

Read more

Problem: I have a class (PortableFoo) designed to be very portable. It contains a scoped class PortableBar. The surrounding codebase (call it Client A) requires both Foo and Bar to have a function that cannot be implemented portably, and Foo’s implementation must call Bar’s implementation. The following is a solution that compiles and works in ..

Read more

Base class: class Base { public: virtual int f() const { return 1; } }; Derived class: class Derived: public Base { public: void f() const {} }; Above code throws a "return type is not identical/covariant error". I’ve read few discussions on it. This one is similar but they only say that it will ..

Read more

I was recently reading about virtual functions and virtual destructors, and the following question aroused. For instance, I have the following inheritance chain. class Base { public: virtual ~Base() // note: virtual { std::cout << "Calling ~Base()n"; } }; class Derived: public Base { public: ~Derived() { std::cout << "Calling ~Derived()n"; } }; I read ..

Read more

I have a parent class and child classes. I want to place objects from these into single vector std::vector<std::shared_ptr<Card> > p1Cards; p1Cards.push_back ( std::shared_ptr<Card> (new Plains("Plain", "Land Card", "White", 0) ) ) ; Note: "Cards" is parent class "Plain" is child class object. I tried this but second line gives me error C++ expected a ..

Read more

To simplify my problem if have this code: class Parent { public: Parent(); ~Parent(); virtual void Func() = 0; }; class Child1 : public Parent{ public: Child1(); void Func() override; … }; class Child2 : public Parent{ public: Child2(); void Func() override; … }; //Some other file std::vector<Parent*> vec; vec.push_back(new Child1()); vec.push_back(new Child2()); std::vector<Parent*> vec2; ..

Read more