Copy Constructors
-
Hi, I've just got Visual Studio 2005 Standard Edition and was writing a new CLR class using this code
#using "System.dll" using namespace System; public ref class SimpleClass { public: SimpleClass(); SimpleClass(System::String^ str,System::Int32 age); ~SimpleClass(); System::String^ GetName() {return _str;} void SetName(System::String^ str) {_str = str;} System::Int32 GetAge() {return _age;} private: System::Int32 _age; System::String^ _str; }; SimpleClass::SimpleClass() {} SimpleClass::~SimpleClass() {} SimpleClass::SimpleClass(System::String^ str,System::Int32 age) { _str = str; _age = age; }
then i wrote a cpp file#include "simpleclass.h" int main() { SimpleClass SC = gcnew SimpleClass("Bob",13); Console::WriteLine(SC.GetAge()); return 0; }
I tried two modifactions both using the new '^' handle operator and normal class by saying SimpleClass SC = gcnew SimpleClass(). However when i tried using "gcnew SimpleClass() it came up with error, no copy constructor. So I wrote this :SimpleClass(const SimpleClass& rhs);
and it came up with several errors. How do you write a copy constructor in C++ 2.0 please :) Thanks :cool: Tom -
Hi, I've just got Visual Studio 2005 Standard Edition and was writing a new CLR class using this code
#using "System.dll" using namespace System; public ref class SimpleClass { public: SimpleClass(); SimpleClass(System::String^ str,System::Int32 age); ~SimpleClass(); System::String^ GetName() {return _str;} void SetName(System::String^ str) {_str = str;} System::Int32 GetAge() {return _age;} private: System::Int32 _age; System::String^ _str; }; SimpleClass::SimpleClass() {} SimpleClass::~SimpleClass() {} SimpleClass::SimpleClass(System::String^ str,System::Int32 age) { _str = str; _age = age; }
then i wrote a cpp file#include "simpleclass.h" int main() { SimpleClass SC = gcnew SimpleClass("Bob",13); Console::WriteLine(SC.GetAge()); return 0; }
I tried two modifactions both using the new '^' handle operator and normal class by saying SimpleClass SC = gcnew SimpleClass(). However when i tried using "gcnew SimpleClass() it came up with error, no copy constructor. So I wrote this :SimpleClass(const SimpleClass& rhs);
and it came up with several errors. How do you write a copy constructor in C++ 2.0 please :) Thanks :cool: Tom>>SimpleClass SC = gcnew SimpleClass();<<
SC
is not a handle type.gcnew
returns a handle. You need to do one of these :-SimpleClass^ SC = gcnew SimpleClass();
orSimpleClass SC; //stack semantics
You don't need a copy constructor. Regards, NishMy blog : Nish’s thoughts on MFC, C++/CLI and .NET
-- modified at 9:20 Friday 13th January, 2006
-
>>SimpleClass SC = gcnew SimpleClass();<<
SC
is not a handle type.gcnew
returns a handle. You need to do one of these :-SimpleClass^ SC = gcnew SimpleClass();
orSimpleClass SC; //stack semantics
You don't need a copy constructor. Regards, NishMy blog : Nish’s thoughts on MFC, C++/CLI and .NET
-- modified at 9:20 Friday 13th January, 2006
Hi Nisht, Thanks for the help! I wanted to use like the class like this. public ref class Class1 { public: void foo() {Console::Writeline("Hello World"); } Class^ C1 = gcnew Class1(); C1->foo(); Class C2; C2.foo() Can that be done with managed classes. Tom
-
Hi Nisht, Thanks for the help! I wanted to use like the class like this. public ref class Class1 { public: void foo() {Console::Writeline("Hello World"); } Class^ C1 = gcnew Class1(); C1->foo(); Class C2; C2.foo() Can that be done with managed classes. Tom
Tom Moore wrote:
Class^ C1 = gcnew Class1(); C1->foo(); Class C2; C2.foo() Can that be done with managed classes.
Yes, that is possible. See my article on stack semantics for more details :- http://www.codeproject.com/managedcpp/cppclidtors.asp[^] Regards, Nish
My blog : Nish’s thoughts on MFC, C++/CLI and .NET