user140053
user140053

Reputation:

value semantic tricks in C++

Apart from C++ (because he has operator overload), do you know another OOP language that can do (or emulate in the worst case) "value semantics" ?

Upvotes: 2

Views: 244

Answers (2)

fredoverflow
fredoverflow

Reputation: 263220

structs in D also have value semantics.

Upvotes: 0

xanatos
xanatos

Reputation: 111890

In C# if you define your "object" as a struct it has value semantic. If you define it as a class it has reference semantic (unless you make it immutable, like string, then its semantic is more similar to the value one).

I'll add that it's quite easy to break this "implicit" semantic.

struct MyStruct
{
    public StringBuilder SB;
}

MyStruct a = new MyStruct();
a.SB = new StringBuilder();

MyStruct b = a;

now you have broken the semantic, because both a and b "point" to the same reference.

Upvotes: 3

Related Questions