Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 485
lezebulon Avatar asked Dec 17 '22 00:12

lezebulon


1 Answers

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() { }
like image 144
JaredPar Avatar answered Jan 06 '23 13:01

JaredPar