我是逆向工程和重新创建一个程序,它实现了全局静态单点。"A“是存储在单例中的类,"B”是单例本身。是否有任何方法使下列代码工作?
template <class TClass>
class B
{
static char cBuffer[sizeof(TClass)];
};
class A : public B<A> {
int a;
int b;
};此代码段将生成以下错误:
<source>:4:22: error: invalid application of 'sizeof' to an incomplete type 'A'
static char cBuffer[sizeof(TClass)];
^~~~~~~~~~~~~~
<source>:7:18: note: in instantiation of template class 'B<A>' requested here
class A : public B<A> {
^
<source>:7:7: note: definition of 'A' is not complete until the closing '}'
class A : public B<A> {
^发布于 2021-07-07 22:18:39
你可以这样做,但这并不理想:
template <class TClass>
class B
{
static char cBuffer[];
};
class A : public B<A> {
int a;
int b;
};
template <>
char B<A>::cBuffer[sizeof(A)];https://stackoverflow.com/questions/68293403
复制相似问题