堆深拷贝上的 C++ 轮数组

标签 c++ arrays heap-memory 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 Error] Question 4.cpp(48): E2285 Could not find a match for 'Wheel::Wheel(const Wheel&)'

在线:

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);

我做错了什么?

最佳答案

那是语法错误,应该是

 Wheel* carWheels = new Wheel[3];

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

考虑分配四 (4) 个车轮,除非您确定您的汽车可以配备三 (3) 个车轮。

关于堆深拷贝上的 C++ 轮数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12136271/

相关文章:

c++ - boost 变体 : How to model JSON?

c++ - 模拟需要直接使用参数的函数的更好方法

c++ - 当 ReferencedDomainName 为 NULL 时出现 LookupAccountName 错误

arrays - Kotlin-在 HashMap 中存储和读取数组

ios - 如何通过标签访问 NSMutablearray 中的对象?

c++ - Boost:从最短路径创建图

php - 从数组中删除非整数项

c++ - 堆分配与 std::vector

我的应用程序在产品中的 Java 堆 Xmx 标志和差异空间的最大大小

c++ - dbgheap.c 抛出访问冲突异常