I've been staring at this for a while and not getting very far. FruitBasketFactory, FruitBasket, and Fruit are three classes from an API I'm using. My goal is to make a fruit basket and then retrieve the fruit. According to the FruitBasketFactory.hpp:
const FruitBasket* getFruitBasket() const;
and then in the FruitBasket.hpp
size_t getNumFruits() const;
const Fruit& operator[](size_t index) const;
So here's my initial code:
FruitBasketFactory fruitBasketFactory;
//since I want an object rather than a pointer I attempt to dereference the pointer
const FruitBasket fruitBasket = *(fruitBasketFactory.getFruitBasket());
But here I get an error "error C2248: 'FruitBasket::FruitBasket' : cannot access private member declared in class 'FruitBasket'". Shouldn't this work?
So, fine... I rework my code.
FruitBasketFactory fruitBasketFactory;
const FruitBasket* fruitBasket = fruitBasketFactory.getFruitBasket();
if(fruitBasket->getNumFruits() > 0) {
//using (*fruitBasket)[0] seems silly
const Fruit fruit = (*fruitBasket)[0];
}
And fail: "error C2248: 'Fruit::Fruit' : cannot access private member declared in class 'Fruit'"
So, another rework
FruitBasketFactory fruitBasketFactory;
const FruitBasket* fruitBasket = fruitBasketFactory.getFruitBasket();
if(fruitBasket->getNumFruits() > 0) {
//this is just ludicrous ... I'm doing something wrong
const Fruit* fruit = &(fruitBasket->operator[](0));
}
As goofy as this piece of code looks, it actually works. But why can I just do what I think should be the most obvious thing?
FruitBasketFactory fruitBasketFactory;
const FruitBasket fruitBasket = *(fruitBasketFactory.getFruitBasket());
if(fruitBasket.getNumFruits() > 0) {
const Fruit fruit = fruitBasket[0];
}
Resolution:
The copy constructors were indeed blocked for both FruitBasket and Fruit. I was able to get around calling them by creating references as follows:
FruitBasketFactory fruitBasketFactory;
const FruitBasket& fruitBasket = *(fruitBasketFactory.getFruitBasket());
if(fruitBasket.getNumFruits() > 0) {
const Fruit& fruit = fruitBasket[0];
}