0

Please take a look at the following:

dispenserType type[4];
dispenserType appleJuice();
type[0] = appleJuice;
dispenserType orangeJuice();
type[1] = orangeJuice;
dispenserType mangoLassi();
type[2] = mangoLassi;
dispenserType fruitPunch();
type[3] = fruitPunch;

as you can see i have stored different objects in the type array.

The dispenserType class has a constructor that sets the number of items to 20. so we have int numberfItems for the object appleJuice set to 20 so on and so forth.

The same class has also the following methods: method that decreases the numberOfItems by one:

void dispenserType::makeSale()
{
    numberOfItems--;
}
int dispenserType::getNoOfItems() const
{
    return numberOfItems;
}

If for example the following is called:

appleJuice.makeSales
cout << appleJuice.getNoOfItems() << endl;

then the program would display the right numberOfItmes, which is 19. On the other hand, if cout<<type[0].getNoOfItems() is called, then it would display the number 20.

That behavior leads me to think that appleJuice and type[0] are two separate objects.

My questions is, is there a way to create an array of objects without needing to declare an object and store in an array as I did it as I did?

thank you.

1
  • Your code shouldn't even compile. Can you post some realistic code, including the declaration of type? Commented Mar 9, 2014 at 8:09

2 Answers 2

1

You are correct, type[0] and appleJuice are two different objects.

Option #1 - You can use an array of objects without cloning each object:

dispenserType type[4];

Option #2 - You can use an array of pointers instead of an array of objects:

dispenserType* type[4];
for (int i=0; i<sizeof(type)/sizeof(*type); i++)
    type[i] = new dispenserType();

And of course, you will have to delete each allocated object at some later point during execution:

for (int i=0; i<sizeof(type)/sizeof(*type); i++)
    delete type[i];
Sign up to request clarification or add additional context in comments.

2 Comments

@user3397856: No problem. Did it help you to work out the problem?
@user3397856: Great, glad to have helped you. You can accept the answer by clicking on the V next to it (only if it indeed answers your question).
1

You are not storing any objects, because you aren't creating any. This is a function declaration:

dispenserType appleJuice();

You need

dispenserType appleJuice;

or

dispenserType appleJuice{}; // C++11

Once you fix that, then yes, doing this

type[0] = appleJuice;

assigns the value of appleJuice to an already existing object type[0]. It is the same as saying

dispenserType a;
dispenserType b;

b = a;  // b and a are different objects

My questions is, is there a way to create an array of objects without needing to declare an object and store in an array as I did it as I did?

For a plain array, the only way is to use an initialization list, because once it is initialized, all you can do is modify its existing elements. So

dispenserType type[3] = { dispenserType(args), 
                          dispenserType(other args), 
                          dispenserType(other args) };

An alternative is to construct an std::vector<dispenserType> and emplace_back elements into it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.