C++学习-2023/12/14-C++类中成员函数的函数指针

#include 

class Book
{
public:
	void print()
	{
		std::cout << name << "\n";
		//留个印象
		auto px = [this]() {std::cout << this->age << "\n"; };
		px();
	}
	//留个印象
	typedef void (Book::* FUNC)();
	void prineData(Book & book, FUNC p)
	{
		(book.*p)();
	}
protected:
	std::string name = "笔记本";  //数据直接初始化
	int age = 10086;
};

int main()
{
	Book book;
	book.print();
	//1.简单用auto自动推断
	auto func = &Book::print; //得到成员函数指针,必加&
	//必须要先做函数指针解析
	(book.*func)(); //()必须要有, *必须要有

	//留个印象 QT最新版本:槽函数
	Book book2;
	book2.prineData(book2, &Book::print);
    std::cout << "Hello World!\n";
}

你可能感兴趣的:(c++,学习,开发语言)