Quick actions

cmd+k|ctrl+k

Navigation

Languages

Whoami revisited

Snippet info

Language

Cpp

Visibility

public

Author

johan.van.breda

Created

2019-07-31T09:33:06Z

Updated

2019-07-31T10:50:45Z

#include <iostream>
#include <vector>
#include <algorithm>

class A 
{
public:
    A() {}
    virtual void whoami() const { std::cout<<"I am an instance of A"<<std::endl; }
    ~A() {}
};

class B : public A
{
public:
    B() : A() {}
    void whoami() const override { std::cout<<"I am an instance of B"<<std::endl; }
    ~B() {}
};

void consume( const A& a )
{
  a.whoami();
}

int main() {
    std::vector<A> a(10,B());
    std::for_each( begin(a),end(a), [](const A& a) {consume(a); } );
    return 0;
}
INFO