簡體   English   中英

C++ 代碼 - 在同一 class 的構造函數中調用一個 class 的構造函數

[英]C++ code -calling a constructor of one class within a constructor of same class

不幸的是,我正在嘗試編譯這個 C++ 代碼,我未能編譯下面的代碼。 你能幫我解釋為什么我收到這個錯誤嗎?

#include <iostream>
#include <cstring>
using namespace std;

class student
{
   private:
    char name[10];
    int id;
    int fee;
       public:
           student(char name[10],int id)
           {
               strcpy(this->name,name);  //string copy
               this->id=id;
               fee=0;
           }
           student(char name[10],int id,int fee)
           {
               student::student(name,id); //calling a constructor of one class 
                                         // within a constructor of same class
               this->fee=fee;
           }
           void show() //print function
           {
               cout<<"Name:"<<name<<endl;
               cout<<"id:"<<id<<endl;
               cout<<"fee:"<<fee<<endl<<endl;
           }
};

int main()
{
 student s1("DAVID",123);
 student s2("WILLIAM",124,5000);
 s1.show();
 s2.show();

    return 0;
}

下面是 GCC 抱怨的錯誤: main.cpp|20|error: cannot call constructor 'student::student' directly [-fpermissive]|

正如@sweenish 正確建議的那樣,您必須在初始化部分執行構造函數委托。 像這樣的東西:

#include <iostream>
#include <cstring>
using namespace std;

class student
{
   private:
    char name[10];
    int id;
    int fee;
       public:
           student(char name[10],int id)
           {
               strcpy(this->name,name);  //string copy
               this->id=id;
               fee=0;
           }
           student(char name[10],int id,int fee): student(name, id) // constructor delegation
           {
               this->fee=fee;
           }
           void show() //print function
           {
               cout<<"Name:"<<name<<endl;
               cout<<"id:"<<id<<endl;
               cout<<"fee:"<<fee<<endl<<endl;
           }
};

int main()
{
 student s1("DAVID",123);
 student s2("WILLIAM",124,5000);
 s1.show();
 s2.show();

    return 0;
}

另外,給你一個建議。 您可以定義更專業的構造函數,並將不那么專業的構造函數的責任委托給更專業的構造函數。

#include <iostream>
#include <cstring>
using namespace std;

class student
{
   private:
    char name[10];
    int id;
    int fee;
       public:
           student(char name[10],int id, int fee): id{id}, fee{fee} // more specialized
           {
               strcpy(this->name,name);  //string copy
           }
           student(char name[10],int id): student(name, id, 0) { } // less specialized calling the more specialized constructor
           void show() //print function
           {
               cout<<"Name:"<<name<<endl;
               cout<<"id:"<<id<<endl;
               cout<<"fee:"<<fee<<endl<<endl;
           }
};

int main()
{
 student s1("DAVID",123);
 student s2("WILLIAM",124,5000);
 s1.show();
 s2.show();

    return 0;
}

這是您的代碼,其中進行了更改以使其編譯; 我用評論標記了更改。

#include <iostream>
// #include <cstring>  // CHANGED: Prefer C++ ways when writing C++
#include <string>  // CHANGED: The C++ way
// using namespace std;  // CHANGED: Bad practice

class student {
 private:
  std::string name{};  // CHANGED: Move from C-string to std::string
  int id = 0;          // CHANGED: Default member initialization
  int fee = 0;

 public:
  // CHANGED: Move from C-string to std::string
  student(std::string name, int id)
      : name(name),
        id(id)
  // CHANGED: ^^ Utilize initialization section
  {
    // CHANGED: Not needed anymore
    //  strcpy(this->name,name);  //string copy
    //  this->id=id;
    //  fee=0;
  }

  student(std::string name, int id, int fee) : student(name, id) {
    // CHANGED: Not needed anymore
    //  student::student(name,id); //calling a constructor of one class
    //                            // within a constructor of same class
    // NOTE: Inconsistency with other ctor w.r.t. this->
    this->fee = fee;
  }

  void show()  // print function
  {
    std::cout << "Name:" << name << '\n';
    std::cout << "id:" << id << '\n';
    std::cout << "fee:" << fee << "\n\n";
  }
};

int main() {
  student s1("DAVID", 123);
  student s2("WILLIAM", 124, 5000);
  s1.show();
  s2.show();

  return 0;
}

這是相同的代碼,但刪除了我注釋掉的內容以使其更易於閱讀。

#include <iostream>
#include <string>

class student {
 private:
  std::string name{};
  int id = 0;
  int fee = 0;

 public:
  student(std::string name, int id) : name(name), id(id) {}

  student(std::string name, int id, int fee) : student(name, id) {
    this->fee = fee;
  }

  void show()
  {
    std::cout << "Name:" << name << '\n';
    std::cout << "id:" << id << '\n';
    std::cout << "fee:" << fee << "\n\n";
  }
};

int main() {
  student s1("DAVID", 123);
  student s2("WILLIAM", 124, 5000);
  s1.show();
  s2.show();

  return 0;
}

初始化部分在參數列表之后並用:標記。 然后按照聲明的順序初始化每個成員。

在構造函數進行委托的情況下,您無法在初始化部分初始化fee 我收到的錯誤是a delegating constructor cannot have other mem-initializers

我不喜歡這樣拆分我的初始化,如果你堅持委托構造函數調用這個 class,實現最具體的構造函數並用你不太具體的構造函數委托給它。 我更喜歡默認成員初始化,因為我認為它會減少混亂和整體編寫代碼。

然后代碼編譯,你會得到預期的 output:

Name:DAVID
id:123
fee:0

Name:WILLIAM
id:124
fee:5000

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM