【发布时间】:2014-11-14 02:56:28
【问题描述】:
如果我们创建一个这样的类:
class Sales_data
{
std::string isbn() const {return bookNo;}
std::string bookNo;
};
我们使一个对象成为整体;
Sales_data total;
total.isbn();
C++ Primer,第五版说(第 258 页),“当我们调用成员函数时,this 被初始化为调用该函数的对象的地址”
,是这样的:
Sales_data::isbn(&total)
而且书也写,我们可以得到书不喜欢:
std::string isbn()const {return this->bookNo;}
我认为隐式参数“this”就像一个指针, 但是我看不到它的类型,有人能帮我指出我的想法有什么问题吗?我应该怎么做才能理解隐式参数'this'和这个参数的作用?
@Jason C 我的额外问题: 这是一个指针,所以它的行为就像一个普通的指针,
#include "iostream"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int a = 1;
int * b = &a;
cout << "the b is " << b << endl;
cout << "the &a is " << &a << endl;
cout << "the *b is " << *b << endl;
cout << "the &b is" << &b << endl;
return 0;
}
在我的电脑上输出是:
the b is 0110FCEC
the &a is 0110FCEC
the *b is 1
the &b is0110FCE0
那么,指针的类型有什么用。
【问题讨论】:
标签: c++