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

Discussion in 'C++' started by bhadorn, Oct 30, 2009.

  1. bhadorn

    bhadorn Guest

    On 30 Okt., 19:10, Johnson <> 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
     
    bhadorn, Oct 30, 2009
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sam pal
    Replies:
    3
    Views:
    556
    E. Robert Tisdale
    Jul 16, 2003
  2. Replies:
    1
    Views:
    589
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=
    Aug 29, 2006
  3. 7stud
    Replies:
    11
    Views:
    700
    Dennis Lee Bieber
    Mar 20, 2007
  4. Jack Bates
    Replies:
    0
    Views:
    282
    Jack Bates
    May 2, 2011
  5. Sam Sungshik Kong

    Value type or reference type

    Sam Sungshik Kong, Jun 2, 2004, in forum: Ruby
    Replies:
    1
    Views:
    103
    Robert Klemme
    Jun 2, 2004
Loading...

Share This Page