Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Copy Constructors

Copy Constructors

Scheduled Pinned Locked Moved Managed C++/CLI
c++csharpdotnetvisual-studiohelp
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tom Moore
    wrote on last edited by
    #1

    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

    N 1 Reply Last reply
    0
    • T Tom Moore

      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

      N Offline
      N Offline
      Nish Nishant
      wrote on last edited by
      #2

      >>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(); or SimpleClass SC; //stack semantics You don't need a copy constructor. Regards, Nish

      My blog : Nish’s thoughts on MFC, C++/CLI and .NET

      -- modified at 9:20 Friday 13th January, 2006

      T 1 Reply Last reply
      0
      • N Nish Nishant

        >>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(); or SimpleClass SC; //stack semantics You don't need a copy constructor. Regards, Nish

        My blog : Nish’s thoughts on MFC, C++/CLI and .NET

        -- modified at 9:20 Friday 13th January, 2006

        T Offline
        T Offline
        Tom Moore
        wrote on last edited by
        #3

        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

        N 1 Reply Last reply
        0
        • T Tom Moore

          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

          N Offline
          N Offline
          Nish Nishant
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups