C++ 如何在派生类中初始化基类staic内联数据成员?
比如说我正在上一堂像这样的课C++ 如何在派生类中初始化基类staic内联数据成员?,c++,templates,static,c++17,class-template,C++,Templates,Static,C++17,Class Template,比如说我正在上一堂像这样的课 #include<iostream> #include<string> template<typename CharT> class base { //..... public: using string_type = std::basic_string<CharT>; static inline string_type var; //..... }; class derived : pub
#include<iostream>
#include<string>
template<typename CharT>
class base
{
//.....
public:
using string_type = std::basic_string<CharT>;
static inline string_type var;
//.....
};
class derived : public base<char>
{
private:
using base<char>::var;
public:
static base<char>::string_type get()
{
return var;
}
};
//template<>
//base<char>::string_type base<char>::var = "Value";
int main()
{
std::cout << derived::get();
}
#包括
#包括
模板
阶级基础
{
//.....
公众:
使用string\u type=std::basic\u字符串;
静态内联字符串类型变量;
//.....
};
派生类:公共基
{
私人:
使用base::var;
公众:
静态基::字符串类型get()
{
收益var;
}
};
//模板
//base::string_type base::var=“Value”;
int main()
{
标准::cout
如何初始化基类内联静态数据成员
删除内联
,您应该
感谢您的回答,我将把public改为protectedin基类:-)
template<typename CharT>
class base
{
public:
using string_type = std::basic_string<CharT>;
static string_type var;
// ^^^^^^^^^^^^^^^^^^^
};
template<>
base<char>::string_type base<char>::var = "Value";
template<>
base<wchar_t>::string_type base<wchar_t>::var = L"Value";
std::cout << base<char>::var;