Thread: Copy Constructor Question

  1. #16
    Registered User
    Join Date
    Feb 2005
    Location
    Seattle, Washington
    Posts
    10
    I went with the "I'm puzzeled as to why what you said should happen isn't happening" approach. It didn't work. I told her about my could, she didn't ask to see it and after I said I used a heap she said that it might being using the default copy constructor because it technically isn't truly dynamic memory. She told me to try it with a linked list instead because it would require a "deep" copy. Good think I wrote a linked list class last quarter. Now I just need to make sure it still works.

  2. #17
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Don't forget the obvious possibility that she knows what she is talking about and you just misunderstood her. If she meant copy assignment operator and you heard copy constructor, then you are going to look pretty foolish when you think you can prove her wrong and it turns out you were the dumb one. I'm not saying you're wrong, just to make sure you are right.

  3. #18
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>Don't forget the obvious possibility that she knows what she is talking about and you just misunderstood her.

    Assuming he didn't hear this wrong:
    >>because it technically isn't truly dynamic memory.

    I'd say it's probably not his fault. The default copy constructor does not exist if you override it with your own explicit copy constructor, and dynamic memory has nothing to do with it at all. While it's true that you CAN use the default copy constructor if there's no dynamic memory (i.e. it's safe to use one, you won't run into memory leaks or premature deletions), the compiler simply is not allowed to ignore a copy constructor you've defined and use a default copy instead.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #19
    Registered User
    Join Date
    Feb 2005
    Location
    Seattle, Washington
    Posts
    10

    The Answer

    Okay so today in class she takes about 30mins or more to explain what she really meant and when it occurs.

    Code:
    object foo(object arg); // returns a type of object.
    
    
    int main()
    {
         object one = stuff;
         object two = foo(one);  
         return 0;
    }
    This red line is supposed to call the copy constructor but doesn't on our compiler due to some optimization(it returns the reference). She says this is what she ment all along, if I understand her correctly. If that was the case though she wouldn't have required the delete statement in the copy constructor.

    Code:
    object foo(object arg); //returns a type of object
    int main()
    {
        object one= stuff;
        object two= stuff;
        two =  foo(one);
        return 0;
    }
    I'm pretty sure that this is what she originaly ment for why I needed the delete for the copy constructor. She no longer thinks this way I think. This for sure calls the assignment operator and she knows this(at least now she does).

  5. #20
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Code:
    object one = stuff;
    This calls the constructor of object that can take whatever type stuff is.

    Code:
    object two = foo(one);
    This calls the copy constructor to create arg from two. No delete is necessary because arg has no former state. The delete is probably harmful.
    Then it calls the copy constructor to create two from the return value. Again, because two has no former state, no delete is necesary. Having one is probably harmful.

    Code:
    two =  foo(one);
    This calls the copy constructor for argument passing. It then calls the copy assignment operater ( operator =(const object &) ) to assign the value of the return object to two. A delete is necessary. However, this does not call the copy constructor, where the delete would be harmful.
    Last edited by CornedBee; 02-19-2005 at 01:28 PM.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #21
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>but doesn't on our compiler due to some optimization
    I'd just like to point out, if this happens then you have a very BAD compiler. The compiler is only allowed to make optimizations IF the changes it makes are guaranteed to have no side-effects on how the program executes (other than speed or memory saving).

    [*snip*]

    CornedBee has pointed out why the delete is never needed in the copy constructor. It is, in short, because constructors are only ever called when an object is being created; and if an object is being created, then it cannot have been formerly created (-> so the dynamic memory cannot have been allocated already).
    Last edited by Hunter2; 02-19-2005 at 02:03 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  7. #22
    Registered User
    Join Date
    Feb 2005
    Location
    Seattle, Washington
    Posts
    10
    Quote Originally Posted by Hunter2
    >>but doesn't on our compiler due to some optimization
    I'd just like to point out, if this happens then you have a very BAD compiler. The compiler is only allowed to make optimizations IF the changes it makes are guaranteed to have no side-effects on how the program executes (other than speed or memory saving).
    Actually I've been told that none of the professors like the optimization of the complier and are trying to figure out why it is set that way.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. copy = concatenate ?
    By Arruba in forum C Programming
    Replies: 3
    Last Post: 11-03-2006, 04:54 PM
  2. calling copy constructor from template
    By Ancient Dragon in forum C++ Programming
    Replies: 3
    Last Post: 09-28-2005, 01:54 PM
  3. dynamic memory alloccation & returning objects
    By haditya in forum C++ Programming
    Replies: 8
    Last Post: 04-21-2005, 11:55 PM
  4. Copy Constructor crashing program
    By bob2509 in forum C++ Programming
    Replies: 5
    Last Post: 11-12-2002, 04:21 PM
  5. Copy Constructor Help
    By Jubba in forum C++ Programming
    Replies: 2
    Last Post: 11-07-2001, 11:15 AM