1
有沒有辦法使用CRTP初始化引用?初始參考成員使用crtp
我的目標是讓喜歡下面的代碼東西合作
#include <iostream>
int gI = 1;
template <typename Derived>
struct A
{
A()
{
static_cast<Derived*>(this)->InitRefs();
}
void InitInt(int & i) { i = gI; }
};
struct B : public A<B>
{
B() : A<B>() {}
void InitRefs()
{
InitInt(i);
}
int & i;
};
int main()
{
B b;
std::cout << b.i;
}