这个指向 class 属性 的指针本身就是一个指针
this pointer pointing to a class property which itself is a pointer
我想知道为什么这段代码无法编译并出现错误,指出 'Member identifier unexpected in function ....' 因为 'this' 指针指向模块本身声明的单个对象,这使得它在这种情况下就像一个隐藏参数因为如果 class 是独立的(即 classes 不继承任何其他 classes),那么使用 'this' 是残留的,并且不能对使用 'this' 明确地。
并且还考虑到使用 'this' 指向指针属性是非法的(因为编译器会抛出错误),为什么编译器只检测用户定义方法的错误(getdata1 (), getdata2(), setdata1(),setdata2()) 并认为构造函数和析构函数没有错误? (好吧,这表明使用 'this' 指向指针属性并不是非法的;这个错误背后一定有某种原因。)
class sampclass {
private:
int *data1,*data2;
public:
sampclass(); // constructor
~sampclass(); // destructor
int getdata1() const {
return this->(*data1); // ERROR: Member identifier unexpected in function sampclass::getdata1() const
}
int getdata2() const {
return this->(*data2); // ERROR: Member identifier unexpected in function sampclass::getdata2() const
}
void setdata1(int data) {
this->(*data1)=data; // ERROR: Member identifier unexpected in function sampclass::setdata1(int)
}
void setdata2(int data) {
this->(*data2)=data; // ERROR: Member identifier unexpected in function sampclass::setdata2(int)
}
};
sampclass::sampclass() {
this->data1=new int(0); // assign default value of 0
this->data2=new int(0);
cout << "\nConstruction Successful\n";
}
sampclass::~sampclass() {
delete this->data1;
delete this->data2;
//data1=NULL;
//data2=NULL;
cout << "\nDestruction Successful\n";
}
int main() {
sampclass *obj=new sampclass;
cout << "data1: " << obj->getdata1() << "\ndata2: " << obj->getdata2() << endl;
obj->setdata1(10);
obj->setdata2(99);
cout << "data1: " << obj->getdata1() << "\ndata2: " << obj->getdata2() << endl;
delete obj;
obj=NULL;
cout << "End of program";
return 0;
}
谁能解释一下编译器抛出这个错误的原因?我相信这背后一定有解释。
请注意,错误是作为评论提及的;当所有 'this->' 从代码中删除(如预期)时,总共抛出 4 个错误,程序编译并按要求运行。
提前致谢。
你的*
放错地方了。在this->X
中,X
必须命名class 中的成员,不能是一些任意表达式。你的代码毫无意义!
改为*(this->data1)
。这将为您提供指针 this->data1
,然后取消引用它。
我想知道为什么这段代码无法编译并出现错误,指出 'Member identifier unexpected in function ....' 因为 'this' 指针指向模块本身声明的单个对象,这使得它在这种情况下就像一个隐藏参数因为如果 class 是独立的(即 classes 不继承任何其他 classes),那么使用 'this' 是残留的,并且不能对使用 'this' 明确地。
并且还考虑到使用 'this' 指向指针属性是非法的(因为编译器会抛出错误),为什么编译器只检测用户定义方法的错误(getdata1 (), getdata2(), setdata1(),setdata2()) 并认为构造函数和析构函数没有错误? (好吧,这表明使用 'this' 指向指针属性并不是非法的;这个错误背后一定有某种原因。)
class sampclass {
private:
int *data1,*data2;
public:
sampclass(); // constructor
~sampclass(); // destructor
int getdata1() const {
return this->(*data1); // ERROR: Member identifier unexpected in function sampclass::getdata1() const
}
int getdata2() const {
return this->(*data2); // ERROR: Member identifier unexpected in function sampclass::getdata2() const
}
void setdata1(int data) {
this->(*data1)=data; // ERROR: Member identifier unexpected in function sampclass::setdata1(int)
}
void setdata2(int data) {
this->(*data2)=data; // ERROR: Member identifier unexpected in function sampclass::setdata2(int)
}
};
sampclass::sampclass() {
this->data1=new int(0); // assign default value of 0
this->data2=new int(0);
cout << "\nConstruction Successful\n";
}
sampclass::~sampclass() {
delete this->data1;
delete this->data2;
//data1=NULL;
//data2=NULL;
cout << "\nDestruction Successful\n";
}
int main() {
sampclass *obj=new sampclass;
cout << "data1: " << obj->getdata1() << "\ndata2: " << obj->getdata2() << endl;
obj->setdata1(10);
obj->setdata2(99);
cout << "data1: " << obj->getdata1() << "\ndata2: " << obj->getdata2() << endl;
delete obj;
obj=NULL;
cout << "End of program";
return 0;
}
谁能解释一下编译器抛出这个错误的原因?我相信这背后一定有解释。 请注意,错误是作为评论提及的;当所有 'this->' 从代码中删除(如预期)时,总共抛出 4 个错误,程序编译并按要求运行。
提前致谢。
你的*
放错地方了。在this->X
中,X
必须命名class 中的成员,不能是一些任意表达式。你的代码毫无意义!
改为*(this->data1)
。这将为您提供指针 this->data1
,然后取消引用它。