相关疑难解决方法(0)

在编译时确定struct成员字节偏移量?

我想在编译时找到struct成员的字节偏移量.例如:

struct vertex_t
{
    vec3_t position;
    vec3_t normal;
    vec2_t texcoord;
}
Run Code Online (Sandbox Code Playgroud)

我想知道字节偏移量normal是(在这种情况下它应该是12.)

我知道我可以使用offsetof,但这是一个运行时功能,我宁愿不使用它.

我正在努力实现甚至可能吗?

编辑:offsetof是编译时,我的坏!

c++ struct offsetof offset compile-time

9
推荐指数
1
解决办法
6716
查看次数

访问私有成员c ++

在此代码中,为什么我可以访问对象的私有成员而没有编译器错误?

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)

c++ private

4
推荐指数
1
解决办法
2867
查看次数

标签 统计

c++ ×2

compile-time ×1

offset ×1

offsetof ×1

private ×1

struct ×1