64,646
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
using namespace std;
class test
{
public:
void hello()
{
cout << "Hello" << endl;
}
};
int main()
{
test *p=new test();
p->hello();
p=NULL;
p->hello();
}
Hello
Hello
#include<iostream>
using namespace std;
class test
{
protected:
int m_nVal;
public:
void hello()
{
m_nVal = 1;
cout << "Hello" << endl;
}
};
int main()
{
test *p=new test();
p->hello();
p=NULL;
p->hello();
}
class test
{
public:
void hello()
{
cout << "Hello" <<Temp<< endl;
}
int Temp;
};
类的成员函数是独立的,只有成员数据都和具体对像有关。
p->hello();这种调用,在编译器里面是转化成这样子的:
hello(test* p);//就是说,这个p只是一个参数,并不是用它的指明函数的地址。函数的地址是已知的。
具体看<深入c++模型>这本书吧[/quote]
好吧~《深入C++对象模型》买了还没看,《C++ Primer》刚看完里面没讲,所以有所疑问。。。class test
{
public:
void hello()
{
cout << "Hello" <<Temp<< endl;
}
int Temp;
};
类的成员函数是独立的,只有成员数据都和具体对像有关。
p->hello();这种调用,在编译器里面是转化成这样子的:
hello(test* p);//就是说,这个p只是一个参数,并不是用它的指明函数的地址。函数的地址是已知的。
具体看<深入c++模型>这本书吧