나는 글로벌 범위에서 변수를 가질 수있는 아이디어를 가지고 놀고 있었지만 not construct them. 새로운 게재 위치가 있다는 점에 유의하십시오. 그러나 나는글로벌 바에 새 배치를 사용할 때 anythig가 올바르지 않습니까?
operator T&() { return *reinterpret_cast<T*>(this); }
대신 내가 this
가에 필요하다고 생각
operator T&() { return reinterpret_cast<T&>(t[0]); }
을 사용하여이 코드이 명확하지 않다
#include <new>
#include <cstdio>
#include <typeinfo>
//#define AlignAs alignas(T)
#define AlignAs
template<class T>struct BlockOf {
AlignAs char t[sizeof(T)];
operator T&() { return reinterpret_cast<T&>(*this); }
~BlockOf(){((T*)&t)->~T(); }
};
struct B{
virtual void v(){}
~B() { printf("B\n"); }
};
struct A: B{
A(){printf("a\n");}
int regularDots;
void v() { printf("A virtual\n"); }
};
BlockOf<A> _a;
A&a=_a;
void init(){
new(&a) A;
}
int main() {
init();
A aa;
a.regularDots=9;
printf("%s %s %d %d\n",
typeid(a).name(),
typeid(aa).name(),
typeid(a).hash_code()==typeid(aa).hash_code(),
sizeof(a) == sizeof(aa)
);
B *b = &a;
b->v();
}
이전 질문에있는 코드가 멤버에 액세스하기 시작한 것이므로 오브젝트를 구성하지 못했습니다. –
@BenVoigt : 예. 그것은 감독이었습니다. 나는 회원들에게 접근 할 계획이 아니었다. 구문을 사용하지 않기 때문에 (oops) 의미가 있습니다. –
나는 당신이하려는 것을보고있다. 전방 선언 된 * 유형의 전역 변수를 갖기 위해 노력하고 있습니다. 전역 참조를 다른 곳에 두는 동안 이니셜 라이저와 저장소를 다른 파일에 붙이고 싶기 때문에 정의를 '포함'할 필요가 없습니다. –