开发者

Any Default Pass By Reference DataTypes in C++?

I understand that unlike C# ( where class is passed by reference and struct is passed by default), both structure and class in C++ are passed by value into a method, unless it is explicitly modified to pass by reference.

i.e.,

// this is a method that passes by reference
void PassReferenceOp(MyClass &myclass, MyStruct &m开发者_开发技巧yStruct)
{
}



// this is a method that passes by value
void PassReferenceOp(MyClass myclass, MyStruct myStruct)
{
}

My question is, is it that there is no data structure, data types in C++ that passes into a method by reference? That no matter I am using std::list, std::list::iterator,std::map, or any data types I create, be it struct or class, all are pass by reference?


There are no default pass by reference types. Of course, there are references which are passed by value (most notably, smart pointers), the difference between which you could argue for a while.

Not that I see the importance. Adding an & is hardly the epitome of difficulty or time wastage.


The closest to what you want is not pass-by-reference but using pointers.

References both in Java and C# are more closely related to pointers than to C++ references: A reference in C++ is an alias to the object, equivalent to the ref keyword in C#, while a reference in C#/Java is an entity that refers (points) to the real instance (as a pointer in C/C++).

The following two are equivalent (assuming a struct type in C#):

void foo( type& param );     // c++
void foo( ref type param );  // c#

Now for reference types, the equivalent would be:

void foo( type* param );     // c++
void foo( type param );      // c#

With the difference that pointers in c++ are not automatically dereferenced, that is you have to use operator-> instead of . to access members.


C++ has different argument passing and assignment semantics. Whether something is passed by value, by reference, or by pointer, is not determined by its type. Argument passing and assignment is always explicit.


Nope, no such type exists.

And (part of) the reason for this is that it just wouldn't make sense in C++. In C++, it makes a big difference whether an object is passed by value or reference, so the function needs to know how its parameter is passed.

In your code snippet, when implementing the first function, we know that we are given two references when the function is called: so any modification we make to these objects will be visible to the caller.

In the second function, we know that we are given two objects by value, meaning we can modify them freely, and that their destructors will be called at the end of the function.

If there was a sneaky type that broke this guarantee, and turned out to be a reference anyway, we could make our code blow up in all sorts of new and exciting ways.

Take a simple template function like this:

template <typename T>
T make_default() { return T(); }

If T is such a hypothetical "reference type", then this would be undefined behavior: it would create a local instance, and then return a reference to it. A reference to the object that just went out of scope and was deleted when the function returned.

Such reference types just don't really work well unless you have a garbage collector and throw away deterministic destruction, which is a pretty major C++ feature.

In C++, ownership is important. In order to write correct code, I need to know who owns every object. When is it created, when is it destroyed? A reference doesn't keep the object it points to alive, so if you're not careful, it can end up pointing to an object that no longer exists, as in my little template example above. We need to know whether or not the object we're working with is a reference.


By default, all C++ parameters are passed by value. Use & to indicate a reference.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜