Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: C++ objects are value type or reference type

Reply
Thread Tools

Re: C++ objects are value type or reference type

 
 
bhadorn
Guest
Posts: n/a
 
      10-30-2009
On 30 Okt., 19:10, Johnson <gpsab...@yahoo.com> wrote:
> 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?


Well your test is not suitable to answer your question. The method
MYTEST takes an object as parameter (the way you describe it).

clTest.i = 2; //assigns a value to the object

clTest2.MYTEST(clTest); //this creates an object on the stack copying
the content from clTest.
-> call of MYTEST

clTest.i = 3; //assigns 3 to the local object
<- removes the object from stack

clTest.i is not changed.

This behavior is typical for method calls with parameter declared as
objects, but it has nothing to do with value or reference types. It's
the way you've declared the method.

To analyse or even to design value or reference types in C++ you must
design the class in the correct way first.

For instance following can not be realy used as value type:

class CTest
{
public:
CTest(){}
void release() { delete this; }
void MYTEST(CTest Test) { ... }

protected:
virtual ~CTest(){}
};

The compiler will not allow you to compile a statement like this

{
CTest clTest;
CTest clTest2;
clTest2.MYTEST(clTest); //<< compile error: can't call
CTest::~CTest() for local parameter object

} //<< compile error: can't call CTest::~CTest() for clTest and
clTest2

Objects of class CTest can not be put on the stack and must be created
with new() on the heap. This can help if a class should work only as a
reference type. But a reference type within C# has more features such
as for instance memory management using garbage collection.

Other topics related to C++ and C#:

http://www.xatlantis.ch/education/interfaces.html
http://www.xatlantis.ch/education/csharp_binding.html

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"raise (type, value, traceback)" and "raise type, value, traceback" Jack Bates Python 0 05-02-2011 05:23 PM
class objects, method objects, function objects 7stud Python 11 03-20-2007 06:05 PM
design patterns: value objects vs. data transfer objects laredotornado@zipmail.com Java 1 08-29-2006 12:14 AM
compiler error: argument of type "VALUE *" is incompatible with parameter of type "VALUE" me2faster@excite.com Ruby 1 05-05-2005 11:23 PM
Value type or reference type Sam Sungshik Kong Ruby 1 06-02-2004 09:48 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57