I'm moving from Java to C++ right now and I'm having some difficulties whenever a commonly used concept in Java doesn't map directly into C++. For instance, in Java I would do something like:
Fruit GetFruit(String fruitName) {
Fruit fruit;
if(fruitName == "apple") fruit = new Fruit("apple");
else if(fruitName == "banana") fruit = new Fruit("banana");
else fruit = new Fruit("kumquat"); //'cause who really wants to eat a kumquat?
return fruit;
}
Of course, in C++ the Fruit fruit;
statement actually creates a fruit. Does this mean I have to have a default constructor? This seems unsafe! What if my default fruit escaped?