2017-01-12 8 views
0

私は一連の継承クラスを処理していますが、テンプレートは保護されていないコンストラクタであり、メンバ変数を持たない純粋な仮想メソッドです。実際に継承されたテンプレートクラスに含まれている必要があります。C++クラスをインスタンス化するときにテンプレート引数にデフォルト値を割り当てる方法

継承バージョンでは、格納されている文字配列のサイズを設定するために、テンプレート引数size_tが1つしかありません。そのため、クラス名の先頭にtemplate<size_t fixed_size>を付ける必要があります。これには2つのプライベートメンバー があり、単一のパラメーターを除く有効なコンストラクターの誰かが渡す文字列のサイズを含みます。だから、私はこの変数をstatic const size_tにすることを考えていました。もう1つの変数はchar[]で、[size_t + 1]に設定されます。私がしたいのは、static const size_tのメンバ変数をインスタンス化自体ではなく、そのコンストラクタが呼び出され、サイズがchar[]char*またはstd::string.size()の長さから来て、その有効なコンストラクタです。 fixed_string<some value> myString("hello");が、私はむしろそれが代わりにこれを行う必要があります:私はコンパイルしようと

#include <string> 

// This base class does not contain any member variables 
// and no implementations of any constructor or function 
// it serves as a definition to your interface as well as 
// defining what methods must be implemented. 
class fixed_string_base { 
protected: 
    // The types of constructors you want to implement 
    explicit fixed_string_base(char words[]) {}; 
    explicit fixed_string_base(const char* words) {} 
    explicit fixed_string_base(const std::string words) {}  

    // The types of things you want to leave to default 
    fixed_string_base() = default; 
    fixed_string_base(fixed_string_base const&) = default; 
    fixed_string_base(fixed_string_base&&) = default; 
    fixed_string_base& operator=(fixed_string_base const&) = default; 
    fixed_string_base& operator=(fixed_string_base&&) = default; 
    virtual ~fixed_string_base() = default; 
public: 
    // Put all of your pure virtual methods here that fixed_string must implement; 
    virtual char* c_str() = 0; 
}; 

// This is the actual class that inherits from its non 
// templated declaration interface. 
template<size_t fixed_size> 
class fixed_string : public fixed_string_base { 
private: 
    static const size_t fixed_string_size_ = fixed_size + 1; 
    char fixed_string_[ fixed_string_size_ ]; 

public: 
    // Experimental not sure how to set the fixed_string_size_ from the size of the array passed in to the constructors 
    explicit fixed_string(char words[]) : fixed_string_size_(sizeof(words) + 1) { 
     fixed_string_ = words; 
     fixed_string_[fixed_size] = '\0'; 
    } 
    explicit fixed_string(const char* words) : fixed_string_size_(sizeof(words) + 1) { 
     fixed_string_ = words; 
     fixed_string_[fixed_size] = '\0'; 
    } 
    explicit fixed_string(const std::string& words) : fixed_string_size(words.size() + 1) { 
     fixed_string_ = words.c_str(); 
     fixed_string_[fixed_size] = '\0'; 
    } 

    virtual char* c_str() { return fixed_string_; } 

    // Defaulted Constructors and Operators 
    fixed_string(fixed_string const&) = default; 
    fixed_string(fixed_string&&) = default; 
    fixed_string& operator=(fixed_string const&) = default; 
    fixed_string& operator=(fixed_string&&) = default; 
    virtual ~fixed_string() = default; 
}; 

fixed_string myString("hello");をここに私のクラスは、現在のように見えるものです私はクラスをインスタンス化するために行くときも、私は以下を行うにはしたくありませんこれらは私が取得していますエラーです:

1>------ Build started: Project: FileTester, Configuration: Debug Win32 ------ 
1> main.cpp 
1>c:\users\skilz80\documents\visual studio 2015\projects\filetester\filetester\main.cpp(71): error C2438: 'fixed_string_size_': cannot initialize static class data via constructor 
1> c:\users\skilz80\documents\visual studio 2015\projects\filetester\filetester\main.cpp(71): note: while compiling class template member function 'fixed_string<5>::fixed_string(const char *)' 
1> c:\users\skilz80\documents\visual studio 2015\projects\filetester\filetester\main.cpp(103): note: see reference to function template instantiation 'fixed_string<5>::fixed_string(const char *)' being compiled 
1> c:\users\skilz80\documents\visual studio 2015\projects\filetester\filetester\main.cpp(103): note: see reference to class template instantiation 'fixed_string<5>' being compiled 
1>c:\users\skilz80\documents\visual studio 2015\projects\filetester\filetester\main.cpp(72): error C3863: array type 'char [6]' is not assignable 
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 

私は一種のコンパイルエラーが明記されているか理解が、私はこのクラスの実装を完了するためにそれらを解決する方法についてはよく分かりません。 auto、またはlambdaの技術のように私に役立つVS2015 CEコンパイラの新しい機能がありますか?私は何が見落としているのか分かりません。すべてのヒント、提案や助けは非常に高く評価されます。

答えて

1

あなたはあなたがあなたがstrncpyをしなければならないとして、C-配列のないコピーが存在しないとして、私は、あなたがあなたのコンストラクタを修正してみましょう

auto s = make_fixed_string("Hello"); 

を行うことができます

template <std::size_t N> 
fixed_string<N> make_fixed_string(const char(&s)[N]) 
{ 
    return fixed_string<N>(s); 
} 

を行うことができます。

+0

ヒープ上に割り当てる必要がないと思っていますが、これが構築されると文字列は固定され、変更できません。その値を返すだけで、 '=='であれば他の固定文字列と比較することができます。 –

+1

私は(あなたのように)割り当てておらず、RVOとコピーelisionはまともなコンパイラでおそらく発生するはずです。また、文字列を 'const'にしたい場合は、' const'を追加することもできます: 'const auto s = make_fixed_string(" Hello ");'。 – Jarod42

+0

私はヒープに割り当てたいことがあるかもしれませんが、私はそうでない時があります。だから私のために自動的にこれを行う便利なクラスを書くことを試みる。基本的に、コンストラクタから渡された値に設定される 'const char [fixed_size]'のラッパーです。 'std :: string'から継承したくないのですが、有効なパラメータまたは入力として' std :: string'を受け入れることは可能です。 –

関連する問題