C ++数组轮上堆深拷贝

时间:2012-08-27 04:35:45

标签: c++ arrays heap deep-copy

这是我的代码:

//---------------------------------------------------------------------------

#pragma hdrstop

#include <tchar.h>
#include <string>
#include <iostream>
#include <sstream>
#include <conio.h>

using namespace std;

//---------------------------------------------------------------------------

class Wheel
{
public:
        Wheel()
    {
        pressure = 32;
        ptrSize = new int(30);
    }
    Wheel(int s, int p)
    {
        ptrSize = new int(s);
        pressure = p;
    }
    ~Wheel()
    {
        delete ptrSize;
    }
    void pump(int amount)
    {
       pressure += amount;
    }

private:
    int *ptrSize;
    int pressure;
};

class RacingCar
{
public:
    RacingCar()
    {
        speed = 0;
        Wheel carWheels = new Wheel[3];
    }
    RacingCar(int s)
    {
        speed = s;
    }
    void Accelerate()
    {
        speed = speed + 10;
    }

private:
    int speed;
};

我使用此代码创建一个RacingCar对象:

RacingCar test();

然而我收到以下错误:

  

[BCC32错误]问题4.cpp(48):E2285找不到匹配'Wheel :: Wheel(c​​onst Wheel&amp;)'

在线:

Wheel carWheels = new Wheel[3];

我想在堆上创建一个包含4个轮子的数组作为对象数组。

我做错了什么?

更新

我想使用类RacingCar的复制构造函数,它将创建RacingCar对象的深层副本,然后编写代码以证明RacingCar对象的副本是深层副本。

我可以帮忙做一下吗?

这是我的代码:

class RacingCar
{
public:
    RacingCar()
    {
        speed = 0;
        Wheel* carWheels = new Wheel[3];
    }
    RacingCar(int s)
    {
        speed = s;
    }
    RacingCar(const RacingCar &oldObject)
    {
        //I am not sure what to place here.
        Wheel* carWheels = new Wheel[3];

    }
    void Accelerate()
    {
        speed = speed + 10;
    }

private:
    int speed;
};

*第二次更新

这是我目前的代码:

class Wheel
{
public:
    Wheel() : pressure(32)
    {
        ptrSize = new int(30);
    }
    Wheel(int s, int p) : pressure(p)
    {
        ptrSize = new int(s);
    }
    ~Wheel()
    {
        delete ptrSize;
    }
    void pump(int amount)
    {
        pressure += amount;
    }
    int getSize()
    {
        return *ptrSize;
    }
    int getPressure()
    {
        return pressure;
    }
private:
    int *ptrSize;
    int pressure;
};

class RacingCar
{
public:
    RacingCar()
    {
        speed = 0;
        *carWheels = new Wheel[4];
    }
    RacingCar(int s)
    {
        speed = s;
    }
    RacingCar(const RacingCar &oldObject)
    {
        for ( int i = 0; i < sizeof(carWheels)/sizeof(carWheels[0]); ++i)
        {
            Wheel oldObjectWheel = oldObject.getWheel(i);
            carWheels[i]=new Wheel(oldObjectWheel.getSize(),oldObjectWheel.getPressure());
        }

    }
    void Accelerate()
    {
        speed = speed + 10;
    }
    Wheel getWheel(int id)
    {
        return *carWheels[id];
    }
private:
    int speed;
    Wheel *carWheels[4];
};

复制构造函数无法正常工作。我收到错误:

Wheel oldObjectWheel = oldObject.getWheel(i);

我做错了什么?

3 个答案:

答案 0 :(得分:3)

这是语法错误,应该是

 Wheel* carWheels = new Wheel[3];

new返回指针类型 - 指向数组中第一个Wheel的指针,因此carWheels[i]按预期方式访问i轮。

考虑分配四(4)个车轮,除非你确定你的车有三(3)个。

答案 1 :(得分:1)

您正在尝试创建一个名为Wheel的{​​{1}},并将其值初始化为指向3 carWheels s数组的指针。你可能想要:

Wheel

答案 2 :(得分:0)

我写了一个深拷贝的例子,希望这有帮助。

//---------------------------------------------------------------------------

#include <iostream>

using namespace std;

//---------------------------------------------------------------------------

class Demo
{
public:
    // Constructor
    Demo()
    {
        ptrNeedsDeepCopy = new int(10);
    }

    // Copy constructor
    Demo(const Demo& rhs)
    {
        // You need to allocate new memory to make sure the two
        // objects are not pointing to the same memeory.
        ptrNeedsDeepCopy = new int(*(rhs.ptrNeedsDeepCopy));
    }

    // Copy assign operator
    Demo& operator=(const Demo& rhs)
    {
        // Do things same in the copy constructor
        // AND!!! you have to check that the object is not
        // assigning it to itself.
        if(this != &rhs)
        {
            ptrNeedsDeepCopy = new int(*(rhs.ptrNeedsDeepCopy));  
        }
        return *this;
    }

    // Destructor
    ~Demo()
    {
        delete ptrNeedsDeepCopy;
    }

    void SomeMemberFunctions()
    {
        // What ever.
    }

    int GetPtrValue()
    {
        if(ptrNeedsDeepCopy)
        {
            return *ptrNeedsDeepCopy;
        }
    }

    void SetPtrValue(int val)
    {
        if(ptrNeedsDeepCopy)
        {
            *ptrNeedsDeepCopy = val;  
        }
    }

private:
    int *ptrNeedsDeepCopy;

};

int main()
{
    Demo a;
    Demo b = a;

    cout << "a's initial value: " << a.GetPtrValue() << endl;
    cout << "b's initial value: " << b.GetPtrValue() << endl;


    a.SetPtrValue(7);

    cout << endl;

    cout << "a's changed value: " << a.GetPtrValue() << endl;
    cout << "b's changed value: " << b.GetPtrValue() << endl;

    return 0;
}