in my little example I have created an array of the size 5 and added 3 car pointers to it. So there are 2 slots unfilled.
Question: How is it possible to find out how many pointers to cars are valid ( 3 would be correct ) without using the std lib? In the example below I tried using a loop and looking for nullptr but the check does not work.
Background: In another program I'm receiving such arrays with a fixed size and pointer to objects and would like to find out how many slots are used.
#include <iostream>
class vehicle
{
public:
int wheels;
};
class car
: public vehicle
{
public:
int power;
int cylinders;
};
int main()
{
int numberOfCars = 5;
car* arrayOfCars[5];
car benz;
car* pBenz =&benz;
benz.cylinders = 8;
benz.power = 350;
car bmw;
car* pBmw = &bmw;
bmw.cylinders = 6;
bmw.power = 240;
car vw;
car* pVw = &vw;
vw.cylinders = 4;
vw.power = 120;
arrayOfCars[0] = pBenz;
arrayOfCars[1] = pBmw;
arrayOfCars[2] = pVw;
for(int idx = 0u; idx < 5; idx++)
{
if( (&arrayOfCars[idx]->cylinders) == nullptr)
{
// do something
}
}
return 0;
}