覆盖语法,专门的模板方法 returns 指向固定大小数组的指针
Override syntax, specialized template method that returns a pointer to a fixed size array
我应该把 override 关键字放在哪里?
Eclipse 索引器在第一个示例中抛出错误,而我的编译器在第二个示例中抛出错误。
基础class:
template<typename type, size_t N>
class Base<type[N]>
{
virtual type const (& Get() const)[N];
};
派生 class:
示例 1) Eclipse 错误。编译器正常
template<typename type, size_t N>
class Derived : Base<type[N]>
{
virtual type const (& Get() const override)[N];
};
示例 2) Eclipse 好的。编译器错误
template<typename type, size_t N>
class Derived : Base<type[N]>
{
virtual type const (& Get() const)[N] override;
};
编辑:堆栈溢出语法突出显示也无法将 override 识别为关键字。
建议:
明智地使用 using
(或 typedef
)让您的生活更轻松:
#include <cstddef>
#include <type_traits>
template<class T> class Base;
template<typename type, size_t N>
class Base<type[N]>
{
public:
using array_type = std::add_const_t<type>[N];
using array_reference = array_type&;
virtual array_reference Get() const;
};
template<class Array>
class Derived : public Base<Array>
{
using base_class = Base<Array>;
public:
using array_reference = typename base_class::array_reference;
virtual array_reference Get() const override;
};
int main()
{
auto x = Derived<int[10]>();
auto br = static_cast<Base<int[10]>&>(x);
auto&& y = br.Get();
}
但要回答您的具体问题:
#include <cstddef>
template<class T> class Base;
template<typename type, size_t N>
class Base<type[N]>
{
public:
virtual type const (& Get() const) [N];
};
template<class T> class Derived;
template<typename type, size_t N>
class Derived<type[N]> : public Base<type[N]>
{
public:
virtual type const (& Get() const override) [N] ;
};
int main()
{
auto x = Derived<int[10]>();
auto br = static_cast<Base<int[10]>&>(x);
auto&& y = br.Get();
}
我应该把 override 关键字放在哪里?
Eclipse 索引器在第一个示例中抛出错误,而我的编译器在第二个示例中抛出错误。
基础class:
template<typename type, size_t N>
class Base<type[N]>
{
virtual type const (& Get() const)[N];
};
派生 class:
示例 1) Eclipse 错误。编译器正常
template<typename type, size_t N>
class Derived : Base<type[N]>
{
virtual type const (& Get() const override)[N];
};
示例 2) Eclipse 好的。编译器错误
template<typename type, size_t N>
class Derived : Base<type[N]>
{
virtual type const (& Get() const)[N] override;
};
编辑:堆栈溢出语法突出显示也无法将 override 识别为关键字。
建议:
明智地使用 using
(或 typedef
)让您的生活更轻松:
#include <cstddef>
#include <type_traits>
template<class T> class Base;
template<typename type, size_t N>
class Base<type[N]>
{
public:
using array_type = std::add_const_t<type>[N];
using array_reference = array_type&;
virtual array_reference Get() const;
};
template<class Array>
class Derived : public Base<Array>
{
using base_class = Base<Array>;
public:
using array_reference = typename base_class::array_reference;
virtual array_reference Get() const override;
};
int main()
{
auto x = Derived<int[10]>();
auto br = static_cast<Base<int[10]>&>(x);
auto&& y = br.Get();
}
但要回答您的具体问题:
#include <cstddef>
template<class T> class Base;
template<typename type, size_t N>
class Base<type[N]>
{
public:
virtual type const (& Get() const) [N];
};
template<class T> class Derived;
template<typename type, size_t N>
class Derived<type[N]> : public Base<type[N]>
{
public:
virtual type const (& Get() const override) [N] ;
};
int main()
{
auto x = Derived<int[10]>();
auto br = static_cast<Base<int[10]>&>(x);
auto&& y = br.Get();
}