Declaring an object before initializing it in c++

前端 未结 10 2201
你的背包
你的背包 2020-12-13 17:17

Is it possible to declare a variable in c++ without instantiating it? I want to do something like this:

Animal a;
if( happyDay() ) 
    a( \"puppies\" ); //c         


        
10条回答
  •  温柔的废话
    2020-12-13 17:45

    You can also use std::move:

    class Ball {
    private:
            // This is initialized, but not as needed
            sf::Sprite ball;
    public:
            Ball() {
                    texture.loadFromFile("ball.png");
                    // This is a local object, not the same as the class member.
                    sf::Sprite ball2(texture);
                    // move it
                    this->ball=std::move(ball2);
            }
    ...
    

提交回复
热议问题