请问能推荐一些用指针解决问题的例子吗,刚学指针,但总感觉不会用,不知如何下手,蟹蟹
用指针指向不同对象例子
#include <Windows.h>
#include <string>
#include <iostream>
using namespace std;
class A
{
public:
virtual string GetName() = 0;
};
class B : public A
{
public:
virtual string GetName()
{
return "B";
}
};
class C : public A
{
public:
virtual string GetName()
{
return "C";
}
};
void main()
{
B b;
C c;
int nIndex;
cin >> nIndex;
A* p = NULL;
if(nIndex == 0)
p = &b;
else
p = &c;
cout << p->GetName().c_str() <<endl;
}
深入理解C指针,看看这本书。