2012-10-28 40 views
1

我想了解C++中的類並開發了一些我在Python中看到的類。下面是代碼:C++:用戶定義類型的構造函數

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

/*============================================================================*/ 
/* Define types 
/*============================================================================*/ 
class none_type; 
class bool_type; 
class int_type; 
struct identifier; 

/*============================================================================*/ 
/* Define none type 
/*============================================================================*/ 
class none_type { 
    public: 
    none_type()     { /* constructor */ }; 
    ~none_type()     { /* destructor */ }; 
}; /* none_type */ 

/*============================================================================*/ 
/* Define bool type 
/*============================================================================*/ 
class bool_type { 
    private: 
    bool base; 
    public: 
    bool_type()     { base = false; }; 
    ~bool_type()     { /* destructor */ }; 
    bool_type(bool init)   { base = bool(init); }; 
    bool_type(int init)   { base = bool(init); }; 
    bool_type(long init)   { base = bool(init); }; 
    bool_type(float init)  { base = bool(init); }; 
    bool_type(double init)  { base = bool(init); }; 
    bool_type(bool_type init) { base = bool(init.base); }; 
    bool_type(int_type init)  { base = bool(init.base); }; 
    int get()     { cout << base << endl; }; 
}; /* bool_type */ 

/*============================================================================*/ 
/* Define int type 
/*============================================================================*/ 
class int_type { 
    private: 
    long base; 
    public: 
    int_type()     { base = 0; }; 
    ~int_type()     { /* destructor */ }; 
    int_type(bool init)   { base = long(init); }; 
    int_type(int init)   { base = long(init); }; 
    int_type(long init)   { base = long(init); }; 
    int_type(float init)   { base = long(init); }; 
    int_type(double init)  { base = long(init); }; 
    int_type(bool_type init)  { base = long(init.base); }; 
    int_type(int_type init)  { base = long(init.base); }; 
    int get()     { cout << base << endl; }; 
}; /* int_type */ 

當我嘗試編譯它,g++告訴我的所有構造函數,其使用自己的類型是無效的。你能解釋一下有什麼不對嗎?我已經定義了類的原型,我還應該做什麼?提前致謝!

+0

你不必把','在函數定義結束。並瞭解構造函數初始化列表... – 2012-10-28 01:03:14

+0

我已經刪除了分號;沒有任何解決辦法。在__必須在C/C++的最後一行中刪除分號時,似乎沒有很多情況。 – ghostmansd

+1

你混淆了「定義」和「聲明」。 –

回答

2

G ++已經告訴你,什麼是錯的:

error: invalid constructor; you probably meant 'bool_type (const bool_type&)'

而不是bool_type (bool_type)您必須使用bool_type (const bool_type&)。原因是,如果你通過值傳遞一個對象,編譯器使用拷貝構造函數將它放在堆棧上。所以,爲了將bool_type傳遞給拷貝構造函數bool_type(bool_type),它必須使用拷貝構造函數本身。這是不可能的。

同樣適用int_type(int_type)

In constructor 'bool_type::bool_type(int_type)': error: 'init' has incomplete type

在這一點上,G ++沒有的int_type的樣子想法。由於它不知道int_type有一個base成員,因此它不能使用它。只是聲明構造:

bool_type(int_type init); 

int_tpye宣佈後定義它:

.... 
class int_type { 
.... 
}; 

... 
inline bool_type(int_type init) { base = bool(init.base); } 

當你有更大的物體,這是很好的建議使用通過引用傳遞,因爲按值方式傳遞複製堆棧上的對象。對於大型對象來說,這要比傳遞對這個大對象的引用昂貴得多。對於小物體,這並不重要。

而最後一個錯誤:

In constructor 'int_type::int_type(bool_type)': error: 'bool bool_type::base' is private

聲明該成員basebool_typeprivate:。這意味着,只有bool_type被允許訪問此成員。爲了獲得base抱着你必須使用的訪問方法get()

int_type(bool_type init)  { base = long(init.get()); } 

類似的,你必須定義:

inline bool_type(int_type init) { base = bool(init.get()); } 

最後,看c++,並按照書單。 C++ FAQ也很體面。

編輯:我錯過了,你的get()方法根本不是訪問器。他們應該被定義爲:

class bool_type { 
public: 
    bool get() const { return base; } 
... 
}; 

同爲int_type::get()

int get() const { return base; } 
3

此構造:

bool_type(int_type init)  { base = bool(init.base); }; 

是無效的,因爲INT_TYPE在這一點上不完整的。

您必須將這個構造實現了類定義的,到了那裏INT_TYPE完成點:

class bool_type { 
    bool_type(int_type init); 
}; 
class int_type {}; 
inline bool_type::bool_type(int_type init)  { base = bool(init.base); }; 

的另一個問題是與你的構造函數,假裝是拷貝構造函數:

bool_type(bool_type init) { base = bool(init.base); }; 

在這裏你有無限遞歸 - 因爲init參數是一個副本 - 所以這個非常構造函數必須被調用來使這個c OPY,但這種構造的該下次調用都有自己的init參數,它必須被複制,並沒有窮盡的或堆棧限制...

拷貝構造函數的適當定義如下:

bool_type(const bool_type& init) { base = bool(init.base); }; 

必須使用const引用,但是在這一點上,您可以依賴編譯器 - 它會爲您生成複製構造函數 - 因此只需將其刪除即可。

1

很難給出「學習C++」的具體意見短,但這裏是如何你的類可以在「正常」 C++設計:

class int_type; 

class none_type { }; 

class bool_type 
{ 
    bool base; 
public: 
    bool_type() : base() { } 
    explicit bool_type(bool init) : base(init) { } 
    explicit bool_type(int_type const &); 

    void print() const { std::cout << base << std::endl; } 
    bool get() const { return base; } 
}; 

class int_type 
{ 
    int base; 
public: 
    int_type() : base() { } 
    explicit int_type(int init) : base(init) { } 
    explicit int_type(bool_type const & init) : base(init.get() ? 1 : 0) { } 

    void print() const { std::cout << base << std::endl; } 
    int get() const { return base; } 
}; 

inline bool_type::bool_type(int_type const & init) : base(init.get()) { } 
+0

'base'在兩種情況下都是私有的。因此,您需要一個訪問器或一個轉換運算符,才能在另一個構造函數中使用它們。 –

+0

@OlafDietsche:好點,固定。謝謝! –