class Dog
{
public:
Dog()
{
m_Name = "小狗狗";
m_Info = "田园狗";
m_Test = "测试";
}
void showTest()
{
cout << "Test:" << m_Test << endl;
}
void showGood()
{
cout << "Good:" << "morning" << endl;
}
public:
std::string m_Name;
std::string m_Info;
private:
std::string m_Test;
void showAgain() // 私有的函数,外部不能赋值给成员函数指针和成员变量
{
cout << "hhh" << endl;
}
};
int main()
{
Dog dog;
unique_ptr<Dog> pDog(new Dog);
/********数据成员指针*********/
string Dog::* p_dog;
p_dog = &Dog::m_Name;
cout << dog.*p_dog << endl; // 使用
auto p_dog_info = &Dog::m_Info;
cout << pDog.get()->*p_dog_info << endl; // 使用
// 数据成员指针只能读取,不能写入
/********成员函数指针*********/
auto p_func = &Dog::showTest;
void(Dog:: * p_func_1)() = &Dog::showGood;
(dog.*p_func)(); // 使用成员函数指针
(pDog.get()->*p_func_1)(); // 使用
return 0;
}
输出:
小狗狗
田园狗
Test:测试
Good:morning