关于使用this指针的问题

发布于 2024-11-30 21:10:06 字数 942 浏览 1 评论 0原文

我尝试使用类内的方法和 this 指针将指针复制到另一个指针,如下所示。我给出了完整的测试代码,以便清楚发生了什么。

class test  {
private:
    int x;
public:
    void setx(int x);
    int getx(void);
    void copy(test *temp);
};

void test::setx(int x)  {
    this->x = x;
}

int test::getx(void)    {
    return this->x;
}

void test::copy(test *temp) {
    this = temp;
}

我从 main 访问此方法,如下所示:

int main()  {
    test a;
    a.setx(4);
    cout << a.getx()<<endl;
    test *b = new test;
    b->setx(4);
    cout << b->getx()<<endl;
    test *c;
    c=b;
    cout << c->getx()<<endl;
    test *d;
    d->copy(b);
    cout << d->getx()<<endl;
}

但是它给出了以下错误

In member function ‘void test::copy(test*)’:
error: lvalue required as left operand of assignment

除了复制部分之外,涉及 this 指针的所有其他方法都工作正常。我在使用 this 指针时犯了一些基本错误吗?

I tried copying a pointer to another by using a method inside the class and the this pointer as follows. I am giving the entire test code so that it is clear what is going on.

class test  {
private:
    int x;
public:
    void setx(int x);
    int getx(void);
    void copy(test *temp);
};

void test::setx(int x)  {
    this->x = x;
}

int test::getx(void)    {
    return this->x;
}

void test::copy(test *temp) {
    this = temp;
}

And I access this method from the main as follows:

int main()  {
    test a;
    a.setx(4);
    cout << a.getx()<<endl;
    test *b = new test;
    b->setx(4);
    cout << b->getx()<<endl;
    test *c;
    c=b;
    cout << c->getx()<<endl;
    test *d;
    d->copy(b);
    cout << d->getx()<<endl;
}

However it gives the following error

In member function ‘void test::copy(test*)’:
error: lvalue required as left operand of assignment

All the other method involving the this pointer works fine except for the copying part. Am i doing some elementary mistake in using the this pointer?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

哑剧 2024-12-07 21:10:06

您无法覆盖this 指针是一个常量,因此您不能更改它。无论如何,这意味着什么?您无法更改所在的对象。您可以更改该对象内的值,但不能更改对象本身。

您需要按值(按对象中存储的内容)复制其他对象,而不是按指针。

另外,您不应该有一个名为 copy 的函数;这就是复制构造函数和复制赋值运算符的用途。

You cannot overwrite this. The this pointer is a constant, so you're not allowed to change it. And what would that mean anyway? You can't change the object that you're in. You can change the values within that object, but not the object itself.

You need to copy other objects by value (by what is stored in the object), not by pointer.

Also, you shouldn't have a function called copy; that's what copy constructors and copy assignment operators are for.

誰認得朕 2024-12-07 21:10:06

您无法修改 this 指针。不过,您可以修改 *this

void test::copy(test *temp)
{
    *this = *temp;
}

此外,您应该重命名数据成员或参数,这样就不需要 this->

class test
{
int m_x;
public:
void setx(int x)
{
    m_x = x;
}

You cannot modify the this pointer. You can however modify *this:

void test::copy(test *temp)
{
    *this = *temp;
}

Also, you should rename the data member or the parameter, so you don't need this->:

class test
{
int m_x;
public:
void setx(int x)
{
    m_x = x;
}
南薇 2024-12-07 21:10:06

test::copy 应该做什么?
显然,您不能为当前对象分配不同的地址。所以是无效的。

如果这应该使用其他对象的值初始化当前对象,那么它应该如下所示:

void test::copy(test *temp) {
    this->x = temp->getX();
}

what is test::copy supposed to do?
Clearly you cant assign a different address to your current object. So it is invalid.

if this is supposed to initialize the current object with the values of some other object then it should look like this:

void test::copy(test *temp) {
    this->x = temp->getX();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文