Thread: Quick ? on copy constructor

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    317

    Quick ? on copy constructor

    Just to make sure I have this right with the whole copy constructor thing:

    you create a class(Example)
    create the copy constructor ( Example(Example & example1) )

    define the copy constructor (
    Example::Example(Example & example1)
    {
    Any data I want the other 'instance?' to have
    }
    )
    then you can create an instance if the class ( Example Ex )
    then you can use the copy constructor ( Example Ex2(Ex) )

    then ex2 would have any data I placed in the copy constructor along with having access to all the functions in Example?

    If I'm totally of the ball here just tell me I'm messed up and I'll go back to beating my head on the keyboard. Any pointers will be greatly appreciated. Thanx

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Yeah... you pretty much have it. Generally, the copy constructor copies stuff from the other argument passed to it, but it doesn't have to.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    317
    Thanx alot, so the copy constructor pretty much is used to copy variables from one instance to another like:
    int variableex2 = variableex?

    I appreciate you guys putting up with my amateur questions.

  4. #4
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Well.. there is no constructor for int, as that is a native type, and not a class, but yeah, you have the idea.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template generated copy constructor
    By AverageSoftware in forum C++ Programming
    Replies: 8
    Last Post: 07-13-2007, 10:51 AM
  2. Copy Constructor
    By noobcpp in forum C++ Programming
    Replies: 3
    Last Post: 07-01-2007, 06:29 AM
  3. Replies: 9
    Last Post: 05-19-2006, 05:19 PM
  4. Copy constructor error, 'this' pointer conversion
    By bennyandthejets in forum C++ Programming
    Replies: 6
    Last Post: 01-08-2004, 05:18 PM
  5. copy constructor
    By ygfperson in forum C++ Programming
    Replies: 6
    Last Post: 03-05-2002, 06:55 PM