Content deleted Content added
→Value types: forgot signature to my answer |
→Value types (again): answer |
||
Line 403:
</source>
::As you can see, zem2 is ''copied'' from zem1, but their "questions" clearly refer to different objects. If they had been copied by reference there would be only one question. [[User:Useerup|Useerup]] ([[User talk:Useerup|talk]]) 07:49, 29 November 2010 (UTC)
:: @Hervegirod : "<i>There is a big difference between C/C++ structures and value types because structures in C or C++ are passed by reference</i>" : You're wrong. In C and C++, you can pass a struct by copy:
<source lang="cpp">
// C++ code
struct Value
{
int i;
double j;
std::string s;
} ;
void foo(Value p_value)
{
// p_value is a copy of the original variable
}
void bar()
{
Value value ;
foo(value) ; // value is passed by copy
}
</source>
::Fact is, in C++, you can pass whatever you want by copy or reference (my personal preferred curiosity being the reference to a pointer) or even const reference. The "Value Type" semantics exists, too, on C++: Anything pointed to could be considered to not be a value type (a pointed type? ... :-) ...), but all this discussion is blurred by the fact you have references or pointers to a value type, even when allocated on the stack:
<source lang="cpp">
// C++ code
void foo()
{
int i = 0 ; // "C# value type"/"Java primitive"
int j = i ; // j is now a copy of i, but a distinct one.
int & ri = i ; // ri is an alias/reference to i
int * pi = &i; // pi points to i
i += 2 ; // now, ri == 2 (and *pi == 2), but j == 0 [value type semantics]
ri += 3 ; // now, i == 5 (and *pi == 5), but j == 0 [value type semantics]
*pi += 4 ; // now, i == 9 (and ri == 9), but j == 0 [value type semantics]
pi += 4 ; // now, pi is invalid [pointer semantics]
}
</source>
:: In C++, the value type (or concrete type) would be something whose meaningful value you access directly.<br />Whereas the pointer type would be something to be access indirectly, through pointer dereferencing, but if you play directly with the pointer (i.e. its address value), then lose its meaningful value (and in the example above, you have a bug). <b>So there is a notion of value types vs. pointed types in C++ (which mirrors the difference between value types and reference types in Java/C#)</b>.<br /><br />[[User:Paercebal|Paercebal]] ([[User talk:Paercebal|talk]]) 15:12, 23 April 2011 (UTC)
== Possible Fallacy in Feature comparison ==
|