Hat*_*end 2 c++ constructor struct
我有以下内容:
int main()
{
struct A
{
unsigned char x, y;
A(unsigned char x, unsigned char y)
{
this.x = x; // Error: expression must have class type.
thix.y = y; // Error: expression must have class type.
}
};
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如何正确指的是x和y的变量struct A,而不是x和y的构造函数的参数变量A?
谢谢.
this 是一个指针,所以你需要取消引用它:
this->x = x;
this->y = y;
Run Code Online (Sandbox Code Playgroud)
如果它是a struct或者无关紧要class,它在两种情况下都是指针.两者之间的唯一不同的是,struct成员是public在默认情况下,当class成员是private默认.
此外,定义函数struct或class函数内部并不是一个好主意.改为在全球范围内.