class 能否使用此指针或其他方式将自身指向不同的地址?
Can a class point itself to a different address using this pointer or in other ways?
class 能否使用例如这个指针?我正在寻找一种在两个对象之间共享数据的方法。例如,如果一个对象的值与另一个对象相同,我会删除一个对象并将两个对象指向相同的数据以保存 space。以下是我在网上找到的示例代码,可以清楚地解决我的问题,但无法编译。
#include<iostream>
using namespace std;
class Test
{
private:
int x;
public:
Test(int x = 0) { this->x = x; delete this;}
void change(Test *t) { this = t; }
void print() { cout << "x = " << x << endl; }
};
int main()
{
Test obj(5);
Test *ptr = new Test (10);
obj.change(ptr);
obj.print();
return 0;
}
错误信息是:
//~>g++ -std=c++14 mm.C -o mm
//mm.C: In member function ‘void Test::change(Test*)’:
//mm.C:10:33: error: lvalue required as left operand of assignment
//void change(Test *t) { this = t; }
没有。 "object" 在 C++ 中的定义是 "a region of storage" (ish)。所以你的对象的存在实际上与 "where it is" 相关联。这就是为什么this
无法修改的原因。
你不能移动它。您可以假装移动它(使用移动语义有效地转移 resources/data 的所有权,然后让原始对象超出范围),但您不能从 class 本身内部执行此操作。
在外部而不是在管理对象的生命周期和其他内容的代码中执行此操作。
不同的对象不能存在于同一个存储中(强调我的):
[intro.object/9]:
Unless an object is a bit-field or a subobject of zero size, the address of that object is the address of the first byte it occupies.
Two objects with overlapping lifetimes that are not bit-fields may have the same address if one is nested within the other, or if at least one is a subobject of zero size and they are of different types; otherwise, they have distinct addresses and occupy disjoint bytes of storage.
现在,您实际上想要做的可能不是让对象它们自己共享相同的存储位置,而是让对象引用相同的 data(正如您自己所说)。这可以通过几种不同的方式实现,但基本思想始终是您(或用户)使用的对象提供对数据的抽象。属于这种模式的一种流行方案是 "copy on write". Also look up the flyweight pattern.
标准库中没有 class 可以直接执行您想要的操作,因此您必须自己实现。 (多个 std::shared_ptr
可以引用同一个数据对象,但是如果数据对象是等价的,则没有内置的方法来合并它们,或者如果试图改变则复制数据。)
class 能否使用例如这个指针?我正在寻找一种在两个对象之间共享数据的方法。例如,如果一个对象的值与另一个对象相同,我会删除一个对象并将两个对象指向相同的数据以保存 space。以下是我在网上找到的示例代码,可以清楚地解决我的问题,但无法编译。
#include<iostream>
using namespace std;
class Test
{
private:
int x;
public:
Test(int x = 0) { this->x = x; delete this;}
void change(Test *t) { this = t; }
void print() { cout << "x = " << x << endl; }
};
int main()
{
Test obj(5);
Test *ptr = new Test (10);
obj.change(ptr);
obj.print();
return 0;
}
错误信息是:
//~>g++ -std=c++14 mm.C -o mm
//mm.C: In member function ‘void Test::change(Test*)’:
//mm.C:10:33: error: lvalue required as left operand of assignment
//void change(Test *t) { this = t; }
没有。 "object" 在 C++ 中的定义是 "a region of storage" (ish)。所以你的对象的存在实际上与 "where it is" 相关联。这就是为什么this
无法修改的原因。
你不能移动它。您可以假装移动它(使用移动语义有效地转移 resources/data 的所有权,然后让原始对象超出范围),但您不能从 class 本身内部执行此操作。
在外部而不是在管理对象的生命周期和其他内容的代码中执行此操作。
不同的对象不能存在于同一个存储中(强调我的):
[intro.object/9]:
Unless an object is a bit-field or a subobject of zero size, the address of that object is the address of the first byte it occupies. Two objects with overlapping lifetimes that are not bit-fields may have the same address if one is nested within the other, or if at least one is a subobject of zero size and they are of different types; otherwise, they have distinct addresses and occupy disjoint bytes of storage.
现在,您实际上想要做的可能不是让对象它们自己共享相同的存储位置,而是让对象引用相同的 data(正如您自己所说)。这可以通过几种不同的方式实现,但基本思想始终是您(或用户)使用的对象提供对数据的抽象。属于这种模式的一种流行方案是 "copy on write". Also look up the flyweight pattern.
标准库中没有 class 可以直接执行您想要的操作,因此您必须自己实现。 (多个 std::shared_ptr
可以引用同一个数据对象,但是如果数据对象是等价的,则没有内置的方法来合并它们,或者如果试图改变则复制数据。)