I've created a BaseClass and two subclasses: SubOne and SubTwo. After that I created a collection called MyCollection which stores the instances in a vector.
Both the base class, and the subclasses have the method getString. Base class returns with base, and the subclasses with sub1 and sub2.
I don't get any warning or error during the compilation. But for some reason, if I try to iterate over the vector, the subclasses return "base"
#include <iostream>
#include <vector>
class BaseClass {
public:
BaseClass() {}
std::string getString() {
return "base";
}
};
class SubOne : public BaseClass {
public:
SubOne() : BaseClass() {}
std::string getString() {
return "sub1";
}
};
class SubTwo : public BaseClass {
public:
SubTwo() : BaseClass() {}
std::string getString() {
return "sub2";
}
};
class MyCollection {
private:
std::vector<BaseClass> instances;
public:
MyCollection() {}
void add(BaseClass & ins) {
instances.push_back(ins);
}
std::string printString() {
for(std::vector<BaseClass>::iterator it = instances.begin() ; it != instances.end(); ++it) {
std::cout << it->getString() << std::endl;
}
}
};
int main() {
MyCollection *coll = new MyCollection();
SubOne* s1 = new SubOne();
SubTwo* s2 = new SubTwo();
coll->add(*s1);
coll->add(*s2);
coll->printString();
return 0;
}
You forgot to use keyword virtual. Also translate to pointers (credit goes to Captain Giraffe).
See code below:
#include <iostream>
#include <vector>
class BaseClass {
public:
BaseClass() {}
virtual std::string getString() { // BINGO _!_!_!_!
return "base";
}
};
class SubOne : public BaseClass {
public:
SubOne() : BaseClass() {}
std::string getString() {
return "sub1";
}
};
class SubTwo : public BaseClass {
public:
SubTwo() : BaseClass() {}
std::string getString() {
return "sub2";
}
};
class MyCollection {
private:
std::vector<BaseClass*> instances;
public:
MyCollection() {}
void add(BaseClass* ins) {
instances.push_back(ins);
}
std::string printString() {
for(std::vector<BaseClass*>::iterator it = instances.begin() ; it != instances.end(); ++it) {
std::cout << (*it)->getString() << std::endl;
}
}
};
int main() {
MyCollection *coll = new MyCollection();
SubOne* s1 = new SubOne();
SubTwo* s2 = new SubTwo();
coll->add(s1);
coll->add(s2);
coll->printString();
return 0;
}
Related
So, I have to create a template class that will store objects of another class, like in a vector or a list. I decided to write a simple example, like an atlas for some animals.
Until now I got this, but I can not instantiate my vector with animals objects. I get this error:
main.cpp|60|error: could not convert <'brace-enclosed initializer list >()' from 'brace-enclosed initializer list>' to 'Animal'
The animal class is just a base class for the other classes like "bird".
#include <iostream>
#include <assert.h>
#include <list>
using namespace std;
class Animal {
protected:
std::string m_name;
Animal (std::string name): m_name {name} {}
public:
virtual std::string regn() const { return "???"; }
virtual ~Animal(){
cout << "Destructor animal"<<'\n';}
};
class Nevertebrate : public Animal{
public:
virtual std::string regn() const { return "nevertebrate";}
virtual ~Nevertebrate();
};
class Vertebrate: public Animal {
protected:
/* std::string m_name;
Vertebrate (std::string name)
:m_name {name} {} */
Vertebrate (std::string name)
: Animal {name} {}
public:
virtual std::string regn() const { return "vertebrate";}
virtual ~Vertebrate(){
cout<<"Destructor vertebrate"<<'\n';};
};
class bird: public Vertebrate {
public:
bird(std::string name)
: Vertebrate{ name }{}
void set_name (std::string nume){
m_name = nume;}
std::string get_name(){
return m_name;}
virtual std::string regn() const {return "pasare";}
virtual ~bird (){
cout << "destructor bird"<<'\n';}
};
template <class T>
class Atlas
{
private:
int m_length{};
T* m_data{};
public:
Atlas(int length)
{
assert(length > 0);
m_data = new T[length]{};
m_length = length;
}
Atlas(const Atlas&) = delete;
Atlas& operator=(const Atlas&) = delete;
~Atlas()
{
delete[] m_data;
}
void erase()
{
delete[] m_data;
m_data = nullptr;
m_length = 0;
}
T& operator[](int index)
{
assert(index >= 0 && index < m_length);
return m_data[index];
}
int getLength() const;
};
template <class T>
int Atlas<T>::getLength() const // note class name is Array<T>, not Array
{
return m_length;
}
int main()
{
Atlas<Animal> AtlasAnimal(10);
return 0;
}
I just learned about the decorator pattern and tried to write an example that uses the code. The example is about beverages and some condiments. Inside the Decorator I have a reference variable to a beverage. The beverages available are Decaf and Espresso. The condiments available are Soy and Caramel. If I define a Decaf with more than one Caramel for example, the result I get is just a Decaf with one decorator. So define Caramel->Caramel->Decaf gives me Caramel->Decaf. Defining Caramel->Soy->Caramel->Decaf works fine. Defining Caramel->Soy->Caramel->Caramel->Decaf gives me Caramel->Soy->Caramel->Decaf. Long story short, I can't have two or more condiments of the same type right one after the other. They become only one condiment. If I use pointers it works fine.
The code:
#include <iostream>
//#include "Decaf.h"
//#include "Espresso.h"
//#include "SoyDecorator.h"
//#include "CaramelDecorator.h"
class Beverage
{
public:
virtual std::string GetDescription() const = 0;
virtual int GetCost() const = 0;
};
class CondimentDecorator : public Beverage
{
public:
Beverage& beverage;
CondimentDecorator(Beverage& beverage) : beverage(beverage) {}
};
class Espresso : public Beverage
{
virtual std::string GetDescription() const override
{
return "Espresso";
}
virtual int GetCost() const override
{
return 5;
}
};
class Decaf : public Beverage
{
virtual std::string GetDescription() const override
{
return "Decaf";
}
virtual int GetCost() const override
{
return 4;
}
};
class CaramelDecorator : public CondimentDecorator
{
public:
CaramelDecorator(Beverage& beverage) : CondimentDecorator(beverage) {}
virtual std::string GetDescription() const override
{
return this->beverage.GetDescription() + " with Caramel";
}
virtual int GetCost() const override
{
return this->beverage.GetCost() + 2;
}
};
class SoyDecorator : public CondimentDecorator
{
public:
SoyDecorator(Beverage& beverage) : CondimentDecorator(beverage) {}
virtual std::string GetDescription() const override
{
return this->beverage.GetDescription() + " with Soy";
}
virtual int GetCost() const override
{
return this->beverage.GetCost() + 1;
}
};
int main()
{
Decaf d;
SoyDecorator s(d);
CaramelDecorator c(s);
CaramelDecorator cc(c);
std::cout << cc.GetDescription() << std::endl;
std::cout << cc.GetCost() << std::endl;
}
output:
Decaf with Soy with Caramel
7
// Expected:
// Decaf with Soy with Caramel with Caramel
// 9
Here is the same code but using pointers and works just fine:
https://ideone.com/7fpGSp
With switching from pointers to references, OPs constructor signature becomes very similar to the (default) copy constructor.
CondimentDecorator(Beverage &beverage) : beverage(beverage) {}
vs.
CondimentDecorator(const Beverage&); // generated by compiler
First, I assumed to delete the copy constructor would be sufficient but the compiler still tries to use the deleted constructor with a respective complaint as it cannot anymore.
Finally, I was able to fix OP's issue with providing the resp. candidates which prevent using the copy constructor.
(Deleting of copy constructor wasn't actually anymore needed but I left it in.)
class CondimentDecorator : public Beverage
{
public:
Beverage& beverage;
CondimentDecorator(Beverage &beverage) : beverage(beverage) {}
CondimentDecorator(CondimentDecorator &beverage) : beverage(beverage) {}
CondimentDecorator(const CondimentDecorator&) = delete;
};
The same has to be done for derived classes:
class CaramelDecorator : public CondimentDecorator
{
public:
CaramelDecorator(Beverage &beverage) : CondimentDecorator(beverage) {}
CaramelDecorator(CaramelDecorator &beverage) : CondimentDecorator(beverage) {}
//CaramelDecorator(const CaramelDecorator&) = delete;
virtual std::string GetDescription() const override
{
return this->beverage.GetDescription() + " with Caramel";
}
virtual int GetCost() const override
{
return this->beverage.GetCost() + 2;
}
};
I fixed only the CaramelDecorator for demo but, actually, this has to be done for all derived classes of class CondimentDecorator.
The fixed MCVE of OP:
#include <iostream>
//#include "Decaf.h"
//#include "Espresso.h"
//#include "SoyDecorator.h"
//#include "CaramelDecorator.h"
class Beverage
{
public:
virtual std::string GetDescription() const = 0;
virtual int GetCost() const = 0;
};
class CondimentDecorator : public Beverage
{
public:
Beverage& beverage;
CondimentDecorator(Beverage &beverage) : beverage(beverage) {}
CondimentDecorator(CondimentDecorator &beverage) : beverage(beverage) {}
CondimentDecorator(const CondimentDecorator&) = delete;
};
class Espresso : public Beverage
{
virtual std::string GetDescription() const override
{
return "Espresso";
}
virtual int GetCost() const override
{
return 5;
}
};
class Decaf : public Beverage
{
virtual std::string GetDescription() const override
{
return "Decaf";
}
virtual int GetCost() const override
{
return 4;
}
};
class CaramelDecorator : public CondimentDecorator
{
public:
CaramelDecorator(Beverage &beverage) : CondimentDecorator(beverage) {}
CaramelDecorator(CaramelDecorator &beverage) : CondimentDecorator(beverage) {}
//CaramelDecorator(const CaramelDecorator&) = delete;
virtual std::string GetDescription() const override
{
return this->beverage.GetDescription() + " with Caramel";
}
virtual int GetCost() const override
{
return this->beverage.GetCost() + 2;
}
};
class SoyDecorator : public CondimentDecorator
{
public:
SoyDecorator(Beverage &beverage) : CondimentDecorator(beverage) {}
virtual std::string GetDescription() const override
{
return this->beverage.GetDescription() + " with Soy";
}
virtual int GetCost() const override
{
return this->beverage.GetCost() + 1;
}
};
int main()
{
Decaf d;
SoyDecorator s(d);
CaramelDecorator c(s);
CaramelDecorator cc(c);
std::cout << cc.GetDescription() << std::endl;
std::cout << cc.GetCost() << std::endl;
}
Output:
Decaf with Soy with Caramel with Caramel
9
Live Demo on coliru
Why the additional candidates are needed?
CondimentDecorator is derived from Beverage.
So, for:
CondimentDecorator d;
CondimentDecorator d2(d);
the compiler has two choices to construct d2:
the custom constructor CondimentDecorator::CondimentDecorator(Beverage &beverage)
the (default) copy constructor CondimentDecorator::CondimentDecorator(const CondimentDecorator&).
For the first, an implicit cast has to be applied but for the copy constructor, no cast is necessary (or at most, a const-cast).
Hence, the compiler prefers the copy constructor (unfortunately even, although it is deleted).
So, another candidate has to be provided which requires as less as implicit casts like the copy constructor:
another custom constructor CondimentDecorator::CondimentDecorator(CondimentDecorator&).
Further reading: Overload Resolution
I wonder where I should use the new and delete in the following class structure:
#include <iostream>
class StateBase {
public:
StateBase( int val ) : m_stateInfo(val) {}
virtual ~StateBase() {}
virtual int getState() = 0;
protected:
int m_stateInfo;
};
class StateImpl1 : public StateBase {
public:
StateImpl1() : StateBase( 1 ) {}
~StateImpl1() {}
int getState() {
//std::cout << "stateimpl:" << m_stateInfo << std::endl;
return m_stateInfo;
}
};
class DeviceBase {
public:
DeviceBase( StateBase* pState ) : mp_state( pState ) {}
~DeviceBase() {} // TODO or delete here? but then it's a bit asymmetric...
virtual void work() = 0;
protected:
StateBase* mp_state;
};
class DeviceImpl1 : public DeviceBase {
public:
DeviceImpl1() : DeviceBase( new StateImpl1() ) {}
// TODO d'tor here? but then base is left without valid ptr to "mp_state"
~DeviceImpl1() { delete mp_state; }
void work() {
std::cout << "DeviceImpl1 work: state = " << mp_state->getState() << std::endl;
}
};
int main() {
DeviceImpl1 impl1;
impl1.work();
return 0;
}
Having the delete in the base class is asymmetric and less readable, I guess.
Using delete in the derived class (where the new is invoked) leaves the base class with a invalid pointer (if only shortly, but it's not clean).
using it in derived class, setting ptr to NULL and and checking pointer for NULL in the base class could be an option
maybe the whole design is flawed? if so: what's an alternative?
Update:
I can not use modern C++ because it will have to be compiled with a cross compiler < C++11.
Use unique_ptr to manage ownership/lifetime/memory:
#include <iostream>
#include <memory>
#include <utility>
class StateBase {
public:
StateBase( int val ) : m_stateInfo(val) {}
virtual ~StateBase() {}
virtual int getState() = 0;
protected:
int m_stateInfo;
};
class StateImpl1 : public StateBase {
public:
StateImpl1() : StateBase( 1 ) {}
~StateImpl1() {}
int getState() {
return m_stateInfo;
}
};
class DeviceBase {
public:
DeviceBase( std::unique_ptr<StateBase> ) : mp_state( std::move(pState) ) {}
virtual ~DeviceBase() {}
virtual void work() = 0;
protected:
std::unique_ptr<StateBase> mp_state;
};
class DeviceImpl1 : DeviceBase {
public:
DeviceImpl1() : DeviceBase( std::make_unique<StateImpl1>() ) {}
~DeviceImpl1() = default;
void work() {
std::cout << "DeviceImpl1 work: state = " << mp_state->getState() << std::endl;
}
};
int main() {
DeviceImpl1 impl1;
impl1.work();
return 0;
}
In modern C++, you would 1. figure out the desired ownership semantics and then 2. express them using appropriate memory management tools, such as smart pointers. Notably, you never manually do memory management. That's effort that should be encapsulated in a class whose sole responsibility is memory management (see single responsibility principle).
#include <iostream>
#include <memory>
class StateBase {
public:
StateBase( int val ) : m_stateInfo(val) {}
virtual ~StateBase() {}
virtual int getState() = 0;
protected:
int m_stateInfo;
};
class StateImpl1 : public StateBase {
public:
StateImpl1() : StateBase( 1 ) {}
~StateImpl1() {}
int getState() {
//std::cout << "stateimpl:" << m_stateInfo << std::endl;
return m_stateInfo;
}
};
class DeviceBase {
public:
// Constructor says: Give me exclusive ownership of a StateBase.
DeviceBase(std::unique_ptr<StateBase> pState) : mp_state(std::move(pState)) {}
// Destructor (implicitly generated) takes care of cleanup.
virtual void work() = 0;
protected:
std::unique_ptr<StateBase> mp_state;
};
class DeviceImpl1 : DeviceBase {
public:
DeviceImpl1() : DeviceBase(std::make_unique<StateImpl1>()) {}
// Base class constructor handles cleanup.
void work() {
std::cout << "DeviceImpl1 work: state = " << mp_state->getState() << std::endl;
}
};
int main() {
DeviceImpl1 impl1;
impl1.work();
return 0;
}
All we did here was to clarify that DeviceBase holds unique ownership of a StateBase. By doing that, we can eliminate all the destructors (std::unique_ptr handles the memory management for us, see also RAII). I would prefer = default; over an empty destructor implementation but that's largely immaterial. The automatically generated destructor of DeviceBase will call the unique_ptr destructor which will delete the stored StateBase instance.
Note that your original code does not follow the rule of three/five/zero, therefore you would run into problems (read: double free etc.) when attempting to copy instances of your class. In the above code using std::unique_ptr, the DeviceBase and DeviceImpl1 classes cannot be copied (because the std::unique_ptr member cannot be copied).
Inherit public base class :
#include <iostream>
class StateBase {
public:
StateBase(int val) : m_stateInfo(val) {}
virtual ~StateBase() {}
virtual int getState() = 0;
protected:
int m_stateInfo;
};
class StateImpl1 : public StateBase {
public:
StateImpl1() : StateBase(1) {}
~StateImpl1() {}
int getState() {
//std::cout << "stateimpl:" << m_stateInfo << std::endl;
return m_stateInfo;
}
};
class DeviceBase {
public:
DeviceBase(StateBase* pState) : mp_state(pState) {}
~DeviceBase() {} // TODO or delete here? but then it's a bit asymmetric...
virtual void work() = 0;
protected:
StateBase* mp_state;
};
class DeviceImpl1 : **public DeviceBase** { // Change is here
public:
DeviceImpl1() : DeviceBase(new StateImpl1()) {}
// TODO d'tor here? but then base is left without valid ptr to "mp_state"
~DeviceImpl1() { delete mp_state; }
void work() {
std::cout << "DeviceImpl1 work: state = " << mp_state->getState() << std::endl;
}
};
How you can initialize the DeviceImpl1 instance impl1 & call delete to execute both of the destructors :
int main() {
DeviceImpl1* impl1 = new DeviceImpl1();
DeviceBase* base = impl1;
impl1->work();
delete base; // This will execute both of the destructor.
return 0;
}
I have an abstract class that has a variable owner_ that is a string. Each derived class declares the name of this variable. Is it better practice to have the variable in the abstract base class, or can I better implement it multiple times in the derived class?
#include <string>
#include <iostream>
class Pet
{
public:
Pet(const std::string& owner) : owner_(owner) {}
virtual ~Pet() = 0;
virtual void print_status() = 0;
protected:
const std::string owner_;
};
Pet::~Pet() {}
class Dog : public Pet
{
public:
Dog(const std::string& owner) : Pet(owner) {}
~Dog() {};
void print_status()
{
std::string s = "Woof! My owner is ";
s += owner_;
std::cout << s << std::endl;
}
// Or better here?
// private:
// const std::string owner_;
};
class Cat : public Pet
{
public:
Cat(const std::string& owner) : Pet(owner) {}
~Cat() {};
void print_status()
{
std::string s = "Meow! My owner is ";
s += owner_;
std::cout << s << std::endl;
}
// Or better here?
// private:
// const std::string owner_;
};
int main()
{
Dog dog("Mario");
dog.print_status();
Cat cat("Luigi");
cat.print_status();
return 0;
}
IMO that's exactly what abstract base classes are for: Provide common implementations for an interface in an inheritance hierarchy.
I'd just go a step further and even separate the interface from the abstract base class:
struct IPet {
virtual ~IPet() = {}
virtual void print_status() = 0;
virtual const std::string& get_owner() const = 0;
};
class Pet : public IPet
{
public:
Pet(const std::string& owner) : owner_(owner) {}
virtual const std::string& get_owner() const { return owner_; }
virtual ~Pet() {} // = 0; Don't declare the destructor as pure virtual function
virtual void print_status() = 0;
protected:
std::string owner_;
};
You might want to use abstract to force your child classes to implement the method but not necessarily define anything in them. If you use them deliberately then having the owner in base class but different content in respective methods is correct.
Abstract methods are being used for example if you want all of your subclasses to at least declare the function inside their own class which is sometimes needed for the different behavior of respective subclass.
class Pet
{
public:
Pet(const std::string& owner) :
owner_(owner) {}
virtual ~Pet() = 0;
virtual void print_status() = 0;
protected:
const std::string owner_;
};
Pet::~Pet() {}
class Dog : public Pet
{
private:
int age;
public:
Dog(const std::string& owner, int age) :
Pet(owner), age(age) {}
~Dog() {};
void print_status(){
std::cout << "Woof! My owner is " << this->owner_ <<
" and my age is " << this->age << "\n\n";
}
};
class Cat : public Pet
{
public:
Cat(const std::string& owner) :
Pet(owner) {}
~Cat() {};
void print_status() {
std::cout << "Miaw, my owner is " << this->owner_ << '\n';
}
};
int main()
{
Dog dog("Mario", 25);
dog.print_status();
Cat cat("Luigi");
cat.print_status();
system("pause");
return 0;
}
I am learning a design pattern, vistor pattern, in c++.
At first, I copy my two practice codes below. First one is "my test code", and the second one is a simplified code of "normal visitor pattern" in my text book. I would like you to read the first code but the second one is just a reference code of normal visitor pattern.
My question is why visitor pattern asks each class to inherit VisitorsHostInterface class which has virtual function, accept(); please refer to the second code, "normal visitor pattern", below if necessary. In my understanding, it is not necessary to use accept() function to scan all instances, like the first code, "my test code".
I suppose "my test code" is simpler than "normal visitor pattern".
Please tell me the reason why visitor pattern asks accept() function to each class? Thank you very much.
(1) my test code
class ClassD;
class ClassC {
public:
ClassC(int new_value, ClassD* new_next_object) : value_(new_value), next_object_(new_next_object) {};
void print() { std::cout << "ClassC value_ = " << value_ << std::endl; }
int value() { return value_; }
std::shared_ptr<ClassD> next_object(void) { return next_object_; }
private:
int value_=0;
std::shared_ptr<ClassD> next_object_;
};
class ClassD {
public:
ClassD(int new_value, ClassC* new_next_object) : value_(new_value), next_object_(new_next_object) {};
void print() { std::cout << "ClassD value_ = " << value_ << std::endl; }
int value() { return value_; }
std::shared_ptr<ClassC> next_object(void) { return next_object_; }
private:
int value_=0;
std::shared_ptr<ClassC> next_object_;
};
class VisitorFuncInterface {
public:
virtual ~VisitorFuncInterface() = default;
virtual void visit(ClassC* obj) = 0;
virtual void visit(ClassD* obj) = 0;
};
class VisitorFunc : public VisitorFuncInterface {
public:
virtual ~VisitorFunc() = default;
void visit(ClassC* obj) {
if (obj) {
obj->print();
this->visit(obj->next_object().get());
}
}
void visit(ClassD* obj) {
if (obj) {
obj->print();
this->visit(obj->next_object().get());
}
}
};
void test_visitor_without_host(void) {
ClassD* d0 = new ClassD(0, nullptr);
ClassC* c0 = new ClassC(1, d0);
ClassD* d1 = new ClassD(2, c0);
VisitorFunc v;
v.visit(d1);
delete d1;
}
The result of test_visitor_without_host() is following,
ClassD value_ = 2
ClassC value_ = 1
ClassD value_ = 0
(2) normal visitor pattern code
class ClassA;
class ClassB;
class VisitorInterface {
public:
virtual ~VisitorInterface() = default;
virtual void visit(ClassA* obj) = 0;
virtual void visit(ClassB* obj) = 0;
};
class VisitorsHostInterface { // = visitor's host
public:
virtual ~VisitorsHostInterface() = default;
virtual void accept(VisitorInterface& v) = 0;
};
class VisitorsHost : public VisitorsHostInterface {
public:
virtual ~VisitorsHost();
void accept(VisitorInterface& v) {};
};
class ClassA : public VisitorsHostInterface {
public:
ClassA(int new_value, ClassB* new_next_object) : value_(new_value), next_object_(new_next_object) {};
void print() { std::cout << "ClassA value_ = " << value_ << std::endl; }
int value() { return value_; }
std::shared_ptr<ClassB> next_object(void) { return next_object_; }
void accept(VisitorInterface& v) { v.visit(this); };
private:
int value_=0;
std::shared_ptr<ClassB> next_object_;
};
class ClassB : public VisitorsHostInterface {
public:
ClassB(int new_value, ClassA* new_next_object) : value_(new_value), next_object_(new_next_object) {};
void print() { std::cout << "ClassB value_ = " << value_ << std::endl; }
int value() { return value_; }
std::shared_ptr<ClassA> next_object(void) { return next_object_; }
void accept(VisitorInterface& v) { v.visit(this); };
private:
int value_=0;
std::shared_ptr<ClassA> next_object_;
};
class Visitor : public VisitorInterface {
public:
virtual ~Visitor() = default;
void visit(ClassA* obj) {
if (obj) {
obj->print();
this->visit(obj->next_object().get());
}
}
void visit(ClassB* obj) {
if (obj) {
obj->print();
this->visit(obj->next_object().get());
}
}
};
void test_visitor(void) {
ClassB* b0 = new ClassB(0, nullptr);
ClassA* a0 = new ClassA(1, b0);
ClassB* b1 = new ClassB(2, a0);
Visitor v;
b1->accept(v);
delete b1;
}
The result of test_visitor() is following,
ClassB value_ = 2
ClassA value_ = 1
ClassB value_ = 0
In your example, you hold all the objects by value and know their static & dynamic type. You don't need dynamic dispatch, so you don't need to have a common VisitorsHostInterface base class. All that's required is for your classes to implement an accept function.
However, the visitor pattern is most commonly used when you don't have access to the dynamic type of the visited objects. Say you have a vecotr<unique_ptr<Widget>>. Where many different Widget sub-types are stored by pointer. Than Widget and every one of its sub classes must implement a virtual accept function. To get to the objects dynamic type, you need to do dynamic dispatch.