其次十章,对象指针及this的使用(C++)

第二十章,对象指针及this的使用(C++)

NPC.h

#ifndef NPC_H
#define NPC_H
#include <string>
class NPC
{
	public:
		std::string name;
		int age;
		void desc();
		 
	protected:
};

#endif

NPC.cpp

#include "npc.h"
#include <iostream>
void NPC::desc(){
	std::cout<<"name:"<<this->name<<",age:"<<this->age<<std::endl;
}
main.cpp

#include <iostream>
#include "NPC.h"

int main(int argc, char** argv) {
	
	NPC n,*npc;
	npc =&n;
	n.age=10;
	npc->name="npc001";
	npc->desc();
	return 0;
}
调试截图:

其次十章,对象指针及this的使用(C++)