CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2004
    Posts
    25

    something wrong with copy constructor

    hi,
    Im just trying to do a program like this:

    List L; // class list object
    List L1(10); //using a constructor
    Lista L=L1; //im trying to call the copy constructor, but after the copy constructor is called, the destructor is called to..

    ps: I could invert like:
    List L1(10);
    List L=L1;
    but im working on a project, thats impossible to do that.

    What can i do to fix it???
    thanks.

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: something wrong with copy constructor

    Post some actual code [your posted code could not possibly compile], along with the class definition....
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  3. #3
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: something wrong with copy constructor

    You've already declared L in the first line. That will default construct it, and you can't then construct it again. Line 3 is a syntax error, because you appear to be trying to redefine L. Having declared L, the best you can do is to copy assign it (using operator=(), which is not the same as copy construct). Getting a destructor call suggests that there's a hidden temporary somewhere, probably caused by a value argument rather than a reference argument. Without the class definitions and function declarations, we can't be any more specific than that.

    Why can't you change it to your second example (which will call the copy ctor)?
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


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