Skip to content

Instantly share code, notes, and snippets.

@rajephon
Created November 29, 2017 00:51
Show Gist options
  • Save rajephon/6a3a2ee51b77f5d635fccbf815db02c7 to your computer and use it in GitHub Desktop.
Save rajephon/6a3a2ee51b77f5d635fccbf815db02c7 to your computer and use it in GitHub Desktop.
C++ virtual inheritance test
class A {
public:
virtual ~A() = default;
virtual void print()=0;
};
class B : public virtual A {
public:
virtual ~B() = default;
virtual void printB()=0;
virtual void print()=0;
};
class C: public virtual A {
public:
virtual ~C() = default;
virtual void printC()=0;
};
class D : public B, public C {
public:
void print() override {
std::cout << "PRINT()\n";
}
void printB() override {
std::cout << "PRINT'B'()\n";
}
void printC() override {
std::cout << "PRINT'C'()\n";
}
D() {
std::cout << "D 생성\n";
}
virtual ~D()=default;
};
void testA(std::shared_ptr<A> a) {
std::shared_ptr<B> b;
b = std::dynamic_pointer_cast<B>(a);
b->printB();
}
int main() {
auto d = std::make_shared<D>();
testA(d);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment