Click to See Complete Forum and Search --> : C++/CLI Copy Constructor and Assignment Operator


sebma
June 12th, 2006, 10:27 AM
Hi ALL,

I am wandering if the copy constructor and = operator for a CLI ref class is implemented correctly below.

// ClrConsole.cpp : main project file.

#include "stdafx.h"

using namespace System;

ref class SomeRef
{
public:
SomeRef(int x, String^ name):m_X(x), m_Name(name) {}

// copy constructor
SomeRef(const SomeRef% rhs)
{
m_X = rhs.m_X;
m_Name = rhs.m_Name;
}

// assignment operator
SomeRef% operator=(const SomeRef% rhs)
{
if (this == %rhs) // prevent assignment to self
return *this;

m_X = rhs.m_X;
m_Name = rhs.m_Name;

return *this;
}

virtual String^ ToString() override
{
return (m_X.ToString() + " " + m_Name);
}

private:
int m_X;
String^ m_Name;
};

void DoSomething(SomeRef ref)
{
Console::WriteLine(L"ref {0}", ref.ToString());
}

int main(array<System::String ^> ^args)
{
SomeRef s1(99, "abc");
SomeRef^ r1 = %s1;

SomeRef s2 = *r1;

Console::WriteLine(L"sr1 {0}", s1.ToString());
Console::WriteLine(L"1 {0}", r1->ToString());
Console::WriteLine(L"sr2 {0}", s2.ToString());

SomeRef^ r2 = gcnew SomeRef(42, "xyz");
DoSomething(*r2);

SomeRef s3(64, "def");
s3 = s3; // assignment to self
Console::WriteLine(L"s3 {0}", s3.ToString());
s3 = *r2;
Console::WriteLine(L"s3 {0}", s3.ToString());

return 0;
}

darwen
June 12th, 2006, 03:44 PM
I've this before, but i'll say it again. There's no real concept of a copy constructor in managed code because all classes are passed by reference.

Darwen.

cilu
June 14th, 2006, 06:42 AM
Perhaps this can help you:

ref class foo
{
public:
foo() {Console::WriteLine("constructor");}

// copy constructor
foo(const foo% rhs)
{
Console::WriteLine("copy constructor");
}

// assignment operator
foo% operator=(const foo% rhs)
{
Console::WriteLine("assignment operator");
if(this == %rhs)
Console::WriteLine("self assignment");
return *this;
}
};


int main(array<System::String ^> ^args)
{
Console::WriteLine("foo f1"); foo f1;
Console::WriteLine("foo f2 = f1"); foo f2 = f1;
Console::WriteLine("foo f3(f2)"); foo f3(f2);
Console::WriteLine("foo^ f4 = gcnew foo()"); foo^ f4 = gcnew foo();
Console::WriteLine("foo^ f5 = gcnew foo(*f4)"); foo^ f5 = gcnew foo(*f4);
Console::WriteLine("foo^ f6 = %f1"); foo^ f6 = %f1;
Console::WriteLine("foo^ f7 = f6"); foo^ f7 = f6;
Console::WriteLine("f1 = f3"); f1 = f3;
Console::WriteLine("f1 = f1"); f1 = f1;
Console::WriteLine("f6 = f7"); f6 = f7;
Console::WriteLine("f7 = f7"); f7 = f7;

return 0;
}

joseph_doggie
May 21st, 2008, 10:45 AM
This is one of the best examples I've ever seen anywhere;
I spent the better part of yesterday searching for something just like this, and it is hard to find.

In any event, great example with both class, main, and output!

This "rocks".

In any event, Microsoft Visual Studio 2008, despite what is said, DOES seem to provide ref classes with default assignment/copy operators, they are sometimes triggered, and provide a default SHALLOW copy.

Thanks!

Ajay Vijay
May 22nd, 2008, 02:19 AM
I've this before, but i'll say it again. There's no real concept of a copy constructor in managed code because all classes are passed by reference.What about "Stack Semantics" in C++/CLI ? :cool: