Jump to content
  • Advertisement
Sign in to follow this  
JohnsonGPS

C++ objects are value type or reference type

This topic is 3302 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I once think C++ objects are reference types, similar as C# objects. If a C++ object are passed to a function, a reference will be saved in the stack, and points to the object in the heap. However, in my test shown below, it seems that C++ objects are actually value type. "clTest2.MYTEST(clTest);" actually triggered "CTest(const CTest &)" to create an internal object. After ""clTest2.MYTEST(clTest);" is "executed,clTest.i" is "2", not "3". Can I say C++ objects are value type? Thank you! class CTest { public: int i; CTest(const CTest &) { i = 1; }; CTest(void) { i = 2; } void MYTEST (CTest clTest) { clTest.i = 3; } }; int main () { CTest clTest; CTest clTest2 = clTest; clTest2.MYTEST(clTest); //Test C++ object }

Share this post


Link to post
Share on other sites
Advertisement
C++ allows you to pass objects by reference or by value.

This passes by value: void myFunction(object myObject);
This passes by reference: void myFunction(object & myObject);

Share this post


Link to post
Share on other sites
Quote:
Original post by Rycross
C++ allows you to pass objects by reference or by value.

This passes by value: void myFunction(object myObject);
This passes by reference: void myFunction(object & myObject);


You are definitely right. But for C#, both of them will be passed by reference, right?

Share this post


Link to post
Share on other sites
The latter syntax isn't even legal C#. C++ and C# are completely different languages.

Objects are just objects in C++, they are neither "value types" nor "reference types" (with the exception of a C++ reference T&, which is effectively another name for an object, since it need not be a complete storage-occupying object in its own right).

Share this post


Link to post
Share on other sites
Quote:
Original post by JohnsonGPS
You are definitely right. But for C#, both of them will be passed by reference, right?


As jpetrie pointed out, the C++ reference syntax isn't even valid in C#. In C#, reference types include anything defined as a class (including boxed primitive types). Value types include structs, enum values, and primitive types. If you want to get technical, the actual references are value types, so if you want to be able to change a reference, you'd have to pass it with the ref keyword.

And, as jpetrie pointed out, the distinction between value and reference types does not exist in C++. If you don't pass by reference or pointer, then a copy will be made (value-type behavior).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!