/*****class Base*****/
class Base
{
public:
Base();
virtual ~Base();
protected:
struct Impl;
Impl* m_Impl;
};
struct Base::Impl
{
int num{ 100 };
};
Base::Base()
{
m_Impl = new Impl;
std::cout << "Base 构造!" << '\n';
}
Base::~Base()
{
if (m_Impl)
{
delete m_Impl;
m_Impl = nullptr;
std::cout << "Base 析构!" << '\n';
}
}
/********class A*********/
class A :public Base
{
public:
A();
~A();
void say();
protected:
struct Impl;
Impl* m_Impl;
};
struct A::Impl
{
std::string name{ "A dog man!" };
};
A::A()
{
m_Impl = new Impl;
std::cout << "A 构造!" << '\n';
}
A::~A()
{
delete m_Impl;
std::cout << "A 析构!" << '\n';
}
void A::say()
{
std::cout << "A say:" << '\n';
std::cout << m_Impl->name << endl;
std::cout << Base::m_Impl->num << endl;
std::cout << '\n';
}
/********class B*********/
class B :public A
{
public:
B();
~B();
void say();
protected:
struct Impl;
Impl* m_Impl;
};
struct B::Impl
{
std::string info{ "B is SB!" };
};
B::B()
{
m_Impl = new Impl;
std::cout << "B 构造!" << '\n';
}
B::~B()
{
delete m_Impl;
std::cout << "B 析构!" << '\n';
}
void B::say()
{
std::cout << "B say:" << '\n';
std::cout << m_Impl->info << endl;
std::cout << A::m_Impl->name << endl;
std::cout << Base::m_Impl->num << endl;
std::cout << '\n';
}
int main()
{
B* b = new B;
std::cout << "-----分割-----" << '\n';
b->say();
std::cout << "-----分割-----" << '\n';
b->A::say();
delete b;
system("pause");
return EXIT_SUCCESS;
}
输出:
Base 构造!
A 构造!
B 构造!
-----分割-----
B say:
B is SB!
A dog man!
100
-----分割-----
A say:
A dog man!
100
B 析构!
A 析构!
Base 析构!
标签:顺序,num,C++,say,Base,-----,析构,Impl
From: https://www.cnblogs.com/huvjie/p/18412939