我想在编译时找到struct成员的字节偏移量.例如:
struct vertex_t
{
vec3_t position;
vec3_t normal;
vec2_t texcoord;
}
Run Code Online (Sandbox Code Playgroud)
我想知道字节偏移量normal是(在这种情况下它应该是12.)
我知道我可以使用offsetof,但这是一个运行时功能,我宁愿不使用它.
我正在努力实现甚至可能吗?
编辑:offsetof是编译时,我的坏!
在此代码中,为什么我可以访问对象的私有成员而没有编译器错误?
class Cents
{
private:
int m_nCents;
public:
Cents(int nCents=0)
{
m_nCents = nCents;
}
// Copy constructor
Cents(const Cents &cSource)
{
m_nCents = cSource.m_nCents;
}
Cents& operator= (const Cents &cSource);
};
Cents& Cents::operator= (const Cents &cSource)
{
Run Code Online (Sandbox Code Playgroud)
cSource.m_nCents是私有的,为什么我可以执行以下操作:
m_nCents = cSource.m_nCents;
// return the existing object
return *this;
}
Run Code Online (Sandbox Code Playgroud)