C ++: creating an uninitialized placeholder variable instead of the default object

I am switching from Java to C ++ right now, and I am having some difficulties when a commonly used concept in Java is not displayed directly in C ++. For example, 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 operator Fruit fruit;actually creates the fetus. Does this mean that I should have a default constructor? This seems unsafe! What if my default fruit escapes?

+5
source share
7 answers

++ , . :

1) Fruit ( ), , :

Fruit GetFruit(const std::string &name)
{
   if ( name == "banana" ) return Fruit("banana");
   if ( name == "apple" )  return Fruit("apple");
   return Fruit("default");
}

2) , , - , ( , noone ):

Fruit* GetFruit(const std::string &name)
{
   if ( name == "banana" ) return new Fruit("banana");
   if ( name == "apple" )  return new Fruit("apple");
   return NULL;
}

3) , , , ( ). Java:

typedef boost::shared_ptr<Fruit> FruitRef;

FruitRef GetFruit(const std::string &name)
{
   if ( name == "banana" ) return new Fruit("banana");
   if ( name == "apple" )  return new Fruit("apple");
   return FruitRef();
}
+9

Java . , . ++ , , .

++:

Fruit * GetFruit(std::string fruitName) {
    Fruit * fruit = 0;
    if (fruitname == "apple") fruit = new Fruit("apple");
    else if (fruitname == "banana") fruit = new Fruit("banana");
    else fruit = new Fruit("kumquat");

    return fruit;
}

Fruit. fruit->color(), fruit.color(). delete , .

+6

:

Fruit GetFruit(String fruitName) {
    if(fruitName == "apple") return Fruit("apple");
    else if(fruitName == "banana") return Fruit("banana");
    else fruit = return Fruit("kumquat"); //'cause who really wants to eat a kumquat?
}

... ( "" ):

auto_ptr<Fruit> GetFruit(String fruitName) {
    auto_ptr<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;
}
+2

++, . ++ (, , ).

   * = 0;   ...

, 0 , .

+1

++ , Java: ( ) . Java ,

MyClass name;

. , , . , , .

++ , 2 :

MyClass object;

. , , . , . , , , :

MyClass *objectPtr = new MyClass();

*, , , .

, , , :

delete objectPtr;

, :

MyClass *yourfunction(bool param) {
    if (param)
        return new MyClass(A);
    return new MyClass(B);
}

, ! , , . : , ?)

-, , :) !

...

+1

fruit java ++. , , , . , fruit Fruit*, ( ). , delete , , new s.

0

Java vs ++. , , , . :

Fruit GetFruit(std::string fruitName) {
    if(fruitName != "apple" && fruitName != "banana")
    {
        fruitName = "kumquat";
    }
    return Fruit(fruitName);
}

However, this code will lead to the fact that the object itself (including all its internal data) will be copied in the return, as well as if it is copied in the future.

To be more Java-esque, you would use instead boost::shared_ptr. Then you deal with a reference counted object, as in Java:

boost::shared_ptr<Fruit> GetFruit(std::string fruitName) {
    if(fruitName != "apple" && fruitName != "banana")
    {
        fruitName = "kumquat";
    }
    return new Fruit(fruitName);
}
0
source

All Articles