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