【问题标题】:Calling static attribute or function in initializer list of constructor在构造函数的初始化列表中调用静态属性或函数
【发布时间】:2016-11-27 22:01:51
【问题描述】:
    01 class Flugzeug {
    02 private:
    03     const unsigned int nr;
    04     unsigned int sitze;
    05     static int serienNr;
    06 public:
    07     Flugzeug(unsigned int sitze);
    08     static int getSerienNr();
    09 };
    10
    11 static int serienNr = 4700;
    12
    13 Flugzeug::Flugzeug(unsigned int sitze) {
    14
    15     this->sitze = sitze;
    16 }
    17 static int getSerienNr(){
    18    return serienNr++;
    19 }

我们必须初始化成员“nr”。 如果我把

14 this->nr = serienNr++;

它会告诉我:

13 error: uninitialized member 'Flugzeug::nr' with 'const' type 'const unsigned int'
14 error: assignment of read-only member 'Flugzeug::nr'

好的,我必须在初始化列表中初始化它吗? 所以我把

13 Flugzeug::Flugzeug(unsigned int sitze) : nr(serienNr++) {

它会告诉我:

13 undefined reference to 'Flugzeug::serienNr'

我都试过了:

13 Flugzeug::Flugzeug(unsigned int sitze) : nr(serienNr++) {
13 Flugzeug::Flugzeug(unsigned int sitze) : nr(getSerienNr()) {
13 Flugzeug::Flugzeug(unsigned int sitze) : nr(Flugzeug::serienNr) {
13 Flugzeug::Flugzeug(unsigned int sitze) : nr(Flugzeug->serienNr) {
13 Flugzeug::Flugzeug(unsigned int sitze) : nr(Flugzeug.serienNr) {
13 Flugzeug::Flugzeug(unsigned int sitze) : nr(this::serienNr) {
13 Flugzeug::Flugzeug(unsigned int sitze) : nr(this->serienNr) {
13 Flugzeug::Flugzeug(unsigned int sitze) : nr(this.serienNr) {
13 Flugzeug::Flugzeug(unsigned int sitze) : nr(Flugzeug::getSerienNr()) {
13 Flugzeug::Flugzeug(unsigned int sitze) : nr(Flugzeug->getSerienNr()) {
13 Flugzeug::Flugzeug(unsigned int sitze) : nr(Flugzeug.getSerienNr()) {
13 Flugzeug::Flugzeug(unsigned int sitze) : nr(this::getSerienNr()) {
13 Flugzeug::Flugzeug(unsigned int sitze) : nr(this->getSerienNr()) {
13 Flugzeug::Flugzeug(unsigned int sitze) : nr(this.getSerienNr()) {

如何用静态成员或静态函数初始化const成员?

谢谢 - Enomine

【问题讨论】:

    标签: function static initialization constants member


    【解决方案1】:

    解决方案:

    #include <iostream>
    #include <cstring>
    using namespace std;
    
    class Flugzeug
    {
    private:
        const unsigned int nr;
        char *typ;
        unsigned int sitze;
        static int serienNr;
    
    public:
        Flugzeug(const char *typ, unsigned int sitze);
        Flugzeug(Flugzeug const &other);
        ~Flugzeug();
        Flugzeug &operator=(Flugzeug const &other);
        unsigned int getNr() const;
        void setTyp(char const *t);
        char *getTyp() const;
        void setSitze(unsigned int sitze);
        unsigned int getSitze() const;
        bool operator==(Flugzeug const &other);
        bool operator<(Flugzeug const &other);
        static int getSerienNr();
    };
    
    int Flugzeug::serienNr = 4700;
    
    char *strdupnew(char const *str)
    {
      return strcpy(new char [strlen(str)+1], str);
    }
    
    Flugzeug::Flugzeug(const char *typ, unsigned int sitze) : nr(serienNr++){
        this->typ = strdupnew(typ);
        this->sitze = sitze;
    }
    Flugzeug::Flugzeug(Flugzeug const &other) : nr(serienNr++){
        this->typ = other.getTyp();
        this->sitze = other.getSitze();
    }
    Flugzeug::~Flugzeug(){
        delete typ;
    }
    Flugzeug &Flugzeug::operator=(Flugzeug const &other) {
        this->typ = other.getTyp();
        this->sitze = other.getSitze();
        return *this;
    }
    unsigned int Flugzeug::getNr() const{
        return nr;
    }
    void Flugzeug::setTyp(char const *t){
        this->typ = strdupnew(t);
    }
    char *Flugzeug::getTyp() const{
        return typ;
    }
    void Flugzeug::setSitze(unsigned int sitze){
        this->sitze = sitze;
    }
    unsigned int Flugzeug::getSitze() const{
        return sitze;
    }
    bool Flugzeug::operator==(Flugzeug const &other){
        return sitze == other.getSitze();
    }
    bool Flugzeug::operator<(Flugzeug const &other){
        return sitze < other.getSitze();
    }
    ostream& operator<<(ostream &out, Flugzeug const &f) {
        out << "Nr: " << f.getNr() << endl;
        out << "Typ: " << f.getTyp() << endl;
        out << "Sitze: " << f.getSitze() << endl;
        out << "SNr: " << f.getSerienNr() << endl;
        return out;
    }
    int Flugzeug::getSerienNr(){
        return serienNr;
    }
    

    【讨论】:

    • 析构函数错误。它应该是delete[] typ; 而不是delete typ;。复制构造函数也是错误的,因为您不复制字符串,只需分配指针。因此,如果另一个实例被删除,该指针现在将导致未定义的行为。
    猜你喜欢
    • 1970-01-01
    • 2021-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-03
    • 1970-01-01
    • 2011-02-15
    • 1970-01-01
    相关资源
    最近更新 更多