CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    May 2005
    Posts
    19

    Copy cons for integer pointer

    Hi All

    class test
    {
    int *i;
    public:

    test(int n)
    {
    i = new int(n)

    }

    test(const test& obj)
    {

    }


    void disp()
    {
    cout<<*i<<endl;
    }
    };

    Please tell me how to write copy constructor for this class



    Thanks

  2. #2
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Copy cons for integer pointer

    Please use code tags when posting code.
    Code:
    test(const test& obj)
    {
      i = new int(*obj.i);
    }
    You will also need a destructor and an implementation of the assignment operator.
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured