c ++如何在实例化类时为模板参数指定默认值

时间:2017-01-12 00:32:10

标签: c++ string templates inheritance char

我处理一组继承的类,基类是非模板,受保护的构造函数和没有成员变量的纯虚方法:它用作实际继承的模板类将要包含的内容的声明。

在继承版本中,它只有一个模板参数size_t,因为这需要设置存储的字符数组的大小,所以我需要让我的类以template<size_t fixed_size>为前缀。它有两个私人成员 一个将包含由其任何一个有效构造函数传入的字符串的大小,该构造函数必须除了单个参数。所以我考虑将此变量设为static const size_t,而另一个仅char[]的变量将设置为[size_t + 1]。我想要做的是在实例化期间不要设置static const size_t的成员变量,但是当它的构造函数被调用并且大小将来自char[]的长度时,{{从其中一个有效构造函数传入的1}}或char*。此外,当我去实例化一个类时,我不想执行以下操作:std::string.size()我宁愿让它这样做:fixed_string<some value> myString( "hello" );这是我的课程目前的样子:

fixed_string 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;
};

我有点理解编译器错误说明了什么但是我不确定如何解决它们来完成实现这个类。 VS2015 CE编译器是否有更新的功能可以帮助我,例如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技术?我不确定我在忽视什么。任何提示,建议或帮助将非常感谢。

1 个答案:

答案 0 :(得分:1)

你可以做到

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

所以你可以做到

auto s = make_fixed_string("Hello");

我允许你修复你的构造函数,因为你没有strncpy,因为没有C数组的副本。