什么条件是默认构造函数生成的? [英] At what condition is the default constructor generated?

查看:192
本文介绍了什么条件是默认构造函数生成的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下类:

  class Tileset {//基类

public:
static std :: vector< Tileset *>列表;
virtual〜Tileset()= 0;

protected:
std :: vector< Tile> tiles_list;
sf ::纹理图;

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

其中 sf :: Texture 构造函数



从我的理解,应该生成一个默认构造函数,因为每个成员也可以默认构造。
当我尝试构造一个派生对象而不调用 Tileset 构造函数时,我有一个编译器错误。
有人可以解释为什么不生成默认构造函数?



编辑:忘记提及 Tile 没有默认构造函数。我不确定这是否会改变任何东西

解决方案

如果下列任何一个是真的, / p>


  • 已声明用户定义的构造函数

  • 类型具有 const 或引用字段



您声明了构造函数,因此C ++不会提供默认生成的构造函数。在这种情况下,虽然 Tileset 的所有字段都有有用的默认构造函数,因此这里定义一个默认构造函数非常容易

  Tileset(){} 


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

解决方案

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() { }

这篇关于什么条件是默认构造函数生成的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆