0

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?

trincot
  • 317,000
  • 35
  • 244
  • 286
Darryl Janecek
  • 399
  • 4
  • 9
  • 25
  • 1
    Don't use raw arrays, they are not first class citizens in C++. Use `std::array` or `std::vector` instead. You will save yourself a lot of trouble. – Fiktik Aug 27 '12 at 08:08
  • 1
    Note that you need to follow the **[Rule of Three](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three)** for your `Wheel` class. – Alok Save Aug 27 '12 at 08:12

3 Answers3

2
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.

marcinj
  • 48,511
  • 9
  • 79
  • 100
  • When passed to a functio, `carWheels` loses size info, so your last expression won't work. And if it's no passed, there's no point to it, since you already know the size. – Luchian Grigore Aug 27 '12 at 08:23
  • Why is the '->' used instead of a '.'? – Darryl Janecek Aug 27 '12 at 08:35
  • @Darryl because `carWheels[0]` is a pointer to `Wheel`. Its the same as if you have `Wheel *myWheel = new Wheel();`, then you can say `myWheel->getPressuire();` or `(*myWheel).getPressure();`. The `->` just improves readability. – Fiktik Aug 27 '12 at 09:36
0

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?

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
0

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.

justin
  • 104,054
  • 14
  • 179
  • 226