Thread: Dynamic copy-constructor...

  1. #1
    Beginning game programmer Petike's Avatar
    Join Date
    Jan 2008
    Posts
    64

    Question Dynamic copy-constructor...

    Hi all,
    I have troubles with a kind of copy-constructor.

    Let's say we want to create a car with 4 wheels.
    So first we declare the class Wheel:
    Code:
    class Wheel
    {
    public:
        Wheel();               // Constructor
        Wheel(const Wheel&);   // Copy-constructor
        ~Wheel();              // Destructor
    }
    Now we declare the class Car:
    Code:
    class Car
    {
    public:
        Car(int numberOfWheels);   // Constructor (specifies the number of wheels of the car)
        Car(const Car&);           // Copy-constructor
        ~Car();                    // Destructor
    private:
        int numberOfWheels;   // Number of wheels
        Wheel* wheels;        // Composition: here will be multiple 'Wheel' objects (dynamically alocated)
    }

    Now the definition of the Car constructor looks like this:
    Code:
    Car::Car(int numberOfWheels) : numberOfWheels(numberOfWheels)
    {
        wheels = new Wheel[numberOfWheels];   // Dynamic allocation of wheels
    }


    So finally let's go to create some car:
    Code:
    int main()
    {
        Car car1(4);   // We have created a car with 4 wheels
    }
    And here are the steps of the car creation:
    1. Car constructor is called.
    2. Wheel constructor is called 4-times.

    And now we would like to copy the existing car to some another car. So we can say:
    Code:
    int main
    {
        Car car1(4);  // We have created a car with 4 wheels
        Car car2 = car1;
    }
    Now the copy-constructor should be called for the copying process (also Car copy-constructor, also Wheel copy-constructors). Thus the Car copy-constructor should also copy the 4 wheels from the old car.



    But my question is:
    =============================================
    What should the Car copy-constructor look like?
    =============================================

    Code:
    Car::Car(const Car&)
    {
        .
        .   ???
        .
    }


    Thanks.

    Petike

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You would need to know how many wheels the car has when you copy it, for one thing. Once you know that, it shouldn't be too difficult to perform the actual memory allocation and copying of the wheel information.

    Also:
    Code:
    Car::Car(int numberOfWheels) : numberOfWheels(numberOfWheels)
    Using exactly the same name of the parameter and the member variable is really a recipe for confusion.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Use a std::vector<Wheel> instead of allocating yourself. It will automatically solve your problem, because vector has value copy semantics.

    It will also solve the copy vs. default-construct/assign problem that you invariably run into when copying a dynamic array.
    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

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Given that Wheel is not a polymorphic type, why doesn't Car make use of a std::vector<Wheel> rather than a pointer and a count of wheels? If you do that, no need to implement a copy constructor for Car at all .... the compiler generated default will work correctly.

    That aside (if you must do this using a pointer to Wheel and a count), you might do this to implement the copy constructor.
    Code:
    Car::Car(const Car&c) : wheels(new Wheel[c.numberOfWheels]), numberOfWheels(c.numberOfWheels)
    {
         for (int i = 0; i < numberOfWheels; ++i)
             wheels[i] = c.wheels[i];
    }
    There are various problems with this approach (you're really better off using a std::vector<Wheel>) but this should be enough to get you started.

  5. #5
    Beginning game programmer Petike's Avatar
    Join Date
    Jan 2008
    Posts
    64
    Ohhh, thank you all very much.
    It works for me now.

    Petike

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. copy array of classes or explicity call constructor
    By Doodle77 in forum C++ Programming
    Replies: 5
    Last Post: 06-10-2007, 11:57 AM
  2. linked list copy constructor
    By brianptodd in forum C++ Programming
    Replies: 3
    Last Post: 04-15-2003, 11:45 AM
  3. copy constructor
    By Eber Kain in forum C++ Programming
    Replies: 1
    Last Post: 09-30-2002, 05:03 PM
  4. copy constructor
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 09-26-2001, 05:17 PM
  5. Using strings with the copy constructor
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 08-29-2001, 03:04 PM