简单明了证明多态虚表是位于常量区

证实虚表存储与常量区

class Person {
public:
	virtual void BuyTicket() { cout << "买票-全价" << endl; }

	virtual void Func1() 
	{
		cout << "Person::Func1()" << endl;
	}

	virtual void Func2() 
	{
		cout << "Person::Func2()" << endl;
	}

//protected:
	int _a = 0;
};

class Student : public Person {
public:
	virtual void BuyTicket() { cout << "买票-半价" << endl; }

private:
	virtual void Func3()
	{
		//_b++;
		cout << "Student::Func3()" << endl;
	}
protected:
	int _b = 1;
};

void Func(Person& p)
{
	p.BuyTicket();
}

int main()
{
	Person ps;
	Student st;

	//栈区
	int a = 0;
	printf("栈区:%p\n", &a);

	//堆区
	int* b = new int;
	printf("堆区:%p\n", b);

	//静态区
	static int c = 0;
	printf("静态区:%p\n", &c);

	//常量区
	const char* s = "hello world";
	printf("常量区:%p\n", s);

	//判断
	printf("判断:%p\n", *((int*)&ps));
	printf("判断:%p\n", *((int*)&st));

	return 0;
}

简单明了证明多态虚表是位于常量区_第1张图片

你可能感兴趣的:(c++,c++)