简体   繁体   中英

C++ Array on the heap

If I declare an array on the heap, how can I get information about the array?

Here is my code:

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

If I have the following:

Wheel *carWheels[4];
*carWheels = new Wheel[4];
cout << carWheels[0].getPressure();

How can I get call the .getPressure() method on any instance in the array when it is on the heap? Also, if I want to create an array of Wheel on the heap, yet use this constructor when creating the array on the heap:

Wheel(int s, int p)

How do I do this?

Wheel *carWheels[4];

is an array of pointers to Wheel, so you need to initialize it with new:

for ( int i = 0; i < sizeof(carWheels)/sizeof(carWheels[0]); ++i)
  carWheels[i]=new Wheel(); // or any other c-tor like Wheel(int s, int p)

later you can access it like that:

carWheels[0]->getPressure();

size of array can be retrieved like above:

sizeof(carWheels)/sizeof(carWheels[0])

[edit - some more details]

If you want to stick to array you will need to pass its size on function call because arrays decays to pointers then. You might want to stay with following syntax:

void func (Wheel* (arr&)[4]){}

which I hope is correct, because I never use it, but better switch to std::vector.

Also with bare pointers in arrays you must remember to delete them at some point, also arrays does not protect you against exceptions - if any will happen you will stay with memory leaks.

Simple, replace

Wheel *carWheels[4];

with

std::vector<Wheel*> carWheels(4);
for ( int i = 0 ; i < 4 ; i++ )
   carWheels[i] = new Wheel(4);

You seem to be confusing () and [] , I suggest you look into that.

You do know that ptrSize = new int(30); doesn't create an array, right?

Like C, you will need to lug the array's element count around with your allocation.

This information is actually stored by the implementation in some cases, but not in a way which is accessible to you.

In C++, we favor types such as std::vector and std::array.


Other notes:

ptrSize = new int(30); << creates one int with a value of 30

How do I do this? Wheel(int s, int p)

Typically, you would just use assignment if you have an existing element:

wheelsArray[0] = Wheel(1, 2);

because you will face difficulty creating an array with a non-default constructor.

and while we're at it:

std::vector<Wheel> wheels(4, Wheel(1, 2));

is all that is needed to create 4 Wheels if you use vector -- no new required. no delete required. plus, vector knows its size.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM