首页 \ 问答 \ 重载C ++赋值运算符以表现得像Java(Overload a C++ assignment operator to behave like Java)

重载C ++赋值运算符以表现得像Java(Overload a C++ assignment operator to behave like Java)

在Java中,当您将一个对象分配给另一个对象时,不会复制原始对象,它只会克隆该引用。 所以,例如,我希望它的行为类似于这个Java代码:

SomeClass x = new SomeClass();
SomeClass y;
y = x; // x is not copied, y is simply a (Java) reference to x

我想要做的是创建一个行为相同的C ++类。 显而易见的选择是重载赋值运算符,如下所示:

SomeClass& operator=(const SomeClass& rhs)
{
    this = &rhs;
    return *this;
}

不幸的是,不允许this分配新位置; 上面的代码甚至不会编译。

有谁知道有任何其他方法来做到这一点?

此外,在你说什么之前:是的,我知道这绝对是实现赋值运算符的错误方法。 无论如何,请放纵我。

编辑2:澄清一下,这里的C ++代码应该像在Java中一样:

SomeClass x = SomeClass(); // Created on stack
SomeClass y; // In C++, same as above; NOT reference or pointer
y = x; // x is not copied, y becomes a (C++) reference to x;
       // original y is destroyed when stack unwinds

我根本不想使用指针。

编辑:我这样做是为了看看我是否可以更改通过引用函数传递的C ++对象的基址。 如果我只是将另一个对象分配给引用变量,它会自动生成一个浅表副本,我不希望这样。 我希望引用参数引用完全不同的对象。

我这样做是为了看看编译器如何实现引用。 如果引用是解除引用指针的语法糖,那么在函数外部,参数对象的基地址不会改变。 如果它们是符号表中的别名(如在PHP中),那么它将会改变。 (因此,使用指针的任何解决方案都已淘汰,因为这是我对测试的“控制”。)

希望有道理。


In Java, when you assign one object to another, the original object isn't copied, it merely clones the reference. So, for example, I'd like it to behave like this Java code:

SomeClass x = new SomeClass();
SomeClass y;
y = x; // x is not copied, y is simply a (Java) reference to x

What I'd like to do is create a C++ class that behaves the same way. The obvious choice is to overload the assignment operator, like so:

SomeClass& operator=(const SomeClass& rhs)
{
    this = &rhs;
    return *this;
}

Unfortunately, assigning a new location to this is not allowed; the above code won't even compile.

Does anyone know of any other way to do this?

Also, before you say anything: yes, I know this is absolutely the wrong way to implement an assignment operator. Please, indulge me anyway.

EDIT 2: To clarify, here is the C++ code that should behave as in Java:

SomeClass x = SomeClass(); // Created on stack
SomeClass y; // In C++, same as above; NOT reference or pointer
y = x; // x is not copied, y becomes a (C++) reference to x;
       // original y is destroyed when stack unwinds

I DO NOT want to use pointers at all.

EDIT: I'm doing this to see if I can change the base address of a C++ object that was passed by reference to a function. If I simply assign another object to the reference variable, it automatically makes a shallow copy, and I don't want that. I want the reference parameter to reference a completely different object.

I'm doing this to see how references are implemented by the compiler. If references are syntactic sugar for dereferenced pointers, then outside the function, the base address of the argument object would NOT change. If they are aliases in the symbol table (as in PHP), then it WILL change. (So any solution using pointers is out, since that's my "control" for the test.)

Hope that makes sense.


原文:https://stackoverflow.com/questions/21176103
更新时间:2023-04-06 11:04

最满意答案

您需要做的就是在li项目中添加一个类,然后在您的css目标中使用不同颜色的类:

$("<li/>", {"id": res[i].id, "text": res[i].content + res[i].status, "class": 'status-' + res[i].status}).appendTo(todo.list);

然后在你的CSS中:

.status-1 { color: red };
.status-2 { color: green };

等(取决于它是文本或背景的颜色)

谢谢


All you would need to do is add a class to the li item, then in your css target this class with the different colour:

$("<li/>", {"id": res[i].id, "text": res[i].content + res[i].status, "class": 'status-' + res[i].status}).appendTo(todo.list);

Then in your css:

.status-1 { color: red };
.status-2 { color: green };

etc (depends if it's color of the text or background)

Thanks

相关问答

更多