Search code examples
c++constructordefault-constructor

At what condition is the default constructor generated?


I have the following class:

class Tileset { //base class

public:
    static std::vector<Tileset*> list;
    virtual ~Tileset() = 0;

protected:
    std::vector<Tile> tiles_list;
    sf::Texture sheet;

private: //non copiable
    Tileset(const Tileset&);
    Tileset& operator=(const Tileset&);
};

where sf::Texture has a default constructor

From my understanding a default constructor should be generated since every member can be default-constructed too. Yet I have a compiler error when I try to construct a derived object without calling a Tileset constructor. Can someone explain why no default constructor is generated?

edit : forgot to mention that Tile class doesn't have a default constructor. I'm not sure if that changes anything


Solution

  • A default constructor will be not be generated if any of the following are true

    • There is a user defined constructor declared
    • The type has a const or reference field

    You declared a constructor hence C++ won't provide a default generated one. In this case though all of the fields of Tileset have useful default constructors so defining a default constructor here is very easy

    Tileset() { }