Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdint.h>
- #include <memory>
- enum ObjectType {
- ObjectTypeA,
- ObjectTypeB,
- ObjectTypeC,
- ObjectTypeD,
- };
- class Base {
- public:
- Base() : x_(0) {}
- virtual ~Base() {}
- virtual bool isOfType(ObjectType type) const { return false; }
- virtual int slowGet(int y) { return x_ + y; }
- bool isA() { return isOfType(ObjectTypeA); }
- bool isB() { return isOfType(ObjectTypeB); }
- bool isC() { return isOfType(ObjectTypeC); }
- protected:
- int x_;
- };
- class Zclass {
- virtual void Hello() { printf("hello!\n"); }
- };
- class A : public Base {
- public:
- bool isOfType(ObjectType type) const override { return type == ObjectTypeA; }
- int slowGet(int y) override { return x_ - y; }
- };
- class B : public Base {
- public:
- bool isOfType(ObjectType type) const override { return type == ObjectTypeB; }
- int slowGet(int y) override { return x_ * y; }
- };
- class D : public A, public Zclass {
- public:
- bool isOfType(ObjectType type) const override { return type == ObjectTypeD ; }
- };
- class C final: public B {
- public:
- bool isOfType(ObjectType type) const override {
- return type == ObjectTypeC || B::isOfType(type);
- }
- int slowGet(int y) override { return x_ + y - 1; }
- };
- __attribute__((noinline)) void printBase(Base* ptr) {
- printf("%d\n", ptr->isOfType(ObjectTypeB));
- }
- __attribute__((noinline)) void printA(A* ptr) {
- printf("%d\n", ptr->isOfType(ObjectTypeD));
- }
- int main() {
- std::unique_ptr<Base> ptr;
- std::unique_ptr<A> ptr2;
- for (int i = 0; i < 4; i++) {
- switch (i) {
- case 0:
- ptr.reset(new A());
- ptr2.reset(new A());
- break;
- case 1:
- ptr.reset(new B());
- ptr2.reset(new A());
- break;
- case 2:
- ptr.reset(new C());
- ptr2.reset(new A());
- break;
- case 3:
- ptr.reset(new D());
- ptr2.reset(new D());
- break;
- }
- printBase(ptr.get());
- printA(ptr2.get());
- printf("\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment