I assume that you are missing the C ++ point and C ++ semantics. You missed the fact. C ++ correctly passes (almost) everything by value because it is the way it is done in C. Always . But not only in C, as I show you below ...
Parameters Semantics in C
In C, everything is passed by value. "primitives" and "POD" are transmitted by copying their values. Change them in your function and the original will not be changed. However, the cost of copying some PODs may not be trivial.
When you use pointer notation (*), you are not following the link. You transmit a copy of the address. Which is more or less the same, but with one subtle difference:
typedef struct { int value ; } P ; /* p is a pointer to P */ void doSomethingElse(P * p) { p->value = 32 ; p = malloc(sizeof(P)) ; /* Don't bother with the leak */ p->value = 45 ; } void doSomething() { P * p = malloc(sizeof(P)) ; p->value = 25 ; doSomethingElse(p) ; int i = p->value ; /* Value of p ? 25 ? 32 ? 42 ? */ }
The final value of p-> is 32. Because p was passed by copying the address value. So the original p was not changed (and the new one was leaked).
Parameters Semantics in Java and C Sharp
This may be surprising to some, but in Java everything is also copied by value. The above C example will give exactly the same results in Java. This is almost what you want, but you won’t be able to pass a "by reference / pointer" primitive as easily as in C.
In C #, they added the ref keyword. It works more or less like a link in C ++. The fact is that in C # you have to mention this both in the function declaration, and with every call. I think this is not what you want.
Parameters Semantics in C ++
In C ++, almost everything is passed by copying the value. When you use nothing but a character type, you copy the character (as is done in C). That is why when you use *, you pass a copy of the address of the character.
But when you use &, then suppose you are passing a real object (be it struct, int, pointer, whatever): Link.
This is easily perceived as syntactic sugar (i.e., behind the scenes, it works like a pointer, and the generated code is the same as for the pointer). But...
The truth is that a link is more than syntactic sugar.
- Unlike pointers, it allows you to manipulate an object as if it were on the stack.
- Separate pointers, if they are associated with the const keyword, it allows implicit progression from one type to another (via constructors, mainly).
- Unlike pointers, a character must not be NULL / invalid.
- Unlike "copies", you do not waste time copying an object
- Unlike by-copy, you can use it as a parameter [out]
- Unlike by-copy, you can use the full range of OOP in C ++ (that is, you pass the complete object to a function that expects an interface).
So links have the best of both worlds.
Let's look at a C example, but with a C ++ version in the doSomethingElse function:
struct P { int value ; } ; // p is a reference to a pointer to P void doSomethingElse(P * & p) { p->value = 32 ; p = (P *) malloc(sizeof(P)) ; // Don't bother with the leak p->value = 45 ; } void doSomething() { P * p = (P *) malloc(sizeof(P)) ; p->value = 25 ; doSomethingElse(p) ; int i = p->value ; // Value of p ? 25 ? 32 ? 42 ? }
The result is 42, and the old p leaked, replaced by the new p. Since, unlike C code, we do not pass a copy of the pointer, but a link to a pointer, that is, a pointer.
When working with C ++, the above example should be clear. If this is not the case, then you are missing something.
Conclusion
C ++ is the value of pass-by-copy / value, because that's how everything works, whether in C, in C # or in Java (even in JavaScript ...: -p ...). And, like C #, C ++ has a reference operator / keyword as a bonus .
Now, as I understand it, you are probably doing what I call semi-jockingly C + , that is, C with some limited C ++ features.
Perhaps your solution uses typedefs (it will amaze your C ++ colleagues, though, to see that the code is dirty with useless typedefs ...), but it will only confuse the fact that you are really absent in C ++.
As another post says, you have to change your mindset from developing C (whatever) to developing in C ++, otherwise you should switch to another language. But do not continue to program the C function with C ++ functions, because deliberately ignoring / obfuscating the power of the idioms you use, you will create suboptimal code.
Note. And do not go with a copy of anything but the primitives. You will drop your function from your OO capacity, but in C ++ this is not what you want.
Edit
The question has been slightly modified (see https://stackoverflow.com/revisions/146271/list ). I gave my initial answer and will answer new questions below.
What do you think of the default semantics in C ++? As you said, this will break compatibility, and you will have a different path for primitives (i.e., built-in types that will still be passed by copy) and structs / objects (which will be passed as links). You would need to add another operator to the "pass-by-value" value (extern "C" is pretty awful and already used for something completely different). No, I really like how it is done today in C ++.
[...] the reference operator seems to me a way to get the address of the variable that I used to get the pointers. I mean, this is the same operator, but with different semantics in different contexts, isn't that too bad for you? Yes and no. The operator → changed its semantics when used with C ++ streams. Then you can use the + = operator to replace strcat. I assume that the operator is also used because its value is “opposite to the pointer” and because they did not want to use one more character (ASCII is limited, and the scope operator :: as well as the pointer → shows that several other characters can be used ) But now, if it bothers you, && will really annoy you, because they added the unary && to C ++ 0x (kind of super-link ...). I have yet to digest it ...