📜  为什么在C++中空类的大小不为零?

📅  最后修改于: 2021-05-25 23:12:03             🧑  作者: Mango

预测以下程序的输出?

CPP
#include
using namespace std;
 
class Empty {};
 
int main()
{
  cout << sizeof(Empty);
  return 0;
}


CPP
#include
using namespace std;
 
class Empty { };
 
int main()
{
    Empty a, b;
 
    if (&a == &b)
      cout << "impossible " << endl;
    else
      cout << "Fine " << endl;
 
   return 0;
}


CPP
#include
using namespace std;
 
class Empty { };
 
int main()
{
    Empty* p1 = new Empty;
    Empty* p2 = new Empty;
 
    if (p1 == p2)
        cout << "impossible " << endl;
    else
        cout << "Fine " << endl;
 
    return 0;
}


CPP
#include
using namespace std;
 
class Empty { };
 
class Derived: Empty { int a; };
 
int main()
{
    cout << sizeof(Derived);
    return 0;
}


CPP
// Thanks to Venki for suggesting this code.
#include 
using namespace std;
 
class Empty {
};
 
class Derived1 : public Empty {
};
 
class Derived2 : virtual public Empty {
};
 
class Derived3 : public Empty {
    char c;
};
 
class Derived4 : virtual public Empty {
    char c;
};
 
class Dummy {
    char c;
};
 
int main()
{
    cout << "sizeof(Empty) " << sizeof(Empty) << endl;
    cout << "sizeof(Derived1) " << sizeof(Derived1) << endl;
    cout << "sizeof(Derived2) " << sizeof(Derived2) << endl;
    cout << "sizeof(Derived3) " << sizeof(Derived3) << endl;
    cout << "sizeof(Derived4) " << sizeof(Derived4) << endl;
    cout << "sizeof(Dummy) " << sizeof(Dummy) << endl;
 
    return 0;
}


输出
1


空类的大小不为零。通常是1个字节。确保两个不同的对象具有不同的地址是非零的。请参见以下示例。

CPP

#include
using namespace std;
 
class Empty { };
 
int main()
{
    Empty a, b;
 
    if (&a == &b)
      cout << "impossible " << endl;
    else
      cout << "Fine " << endl;
 
   return 0;
}
输出
Fine 



由于相同的原因(不同的对象应该具有不同的地址),“ new”总是返回指向不同对象的指针。请参见以下示例。

CPP

#include
using namespace std;
 
class Empty { };
 
int main()
{
    Empty* p1 = new Empty;
    Empty* p2 = new Empty;
 
    if (p1 == p2)
        cout << "impossible " << endl;
    else
        cout << "Fine " << endl;
 
    return 0;
}
输出
Fine 



现在猜测以下程序的输出(这很棘手)

CPP

#include
using namespace std;
 
class Empty { };
 
class Derived: Empty { int a; };
 
int main()
{
    cout << sizeof(Derived);
    return 0;
}
输出
4


请注意,输出不大于4。有一个有趣的规则,说空的基类不需要用单独的字节表示。因此,在基类为空的情况下,编译器可以自由进行优化。作为练习,请在编译器上尝试以下程序。

CPP

// Thanks to Venki for suggesting this code.
#include 
using namespace std;
 
class Empty {
};
 
class Derived1 : public Empty {
};
 
class Derived2 : virtual public Empty {
};
 
class Derived3 : public Empty {
    char c;
};
 
class Derived4 : virtual public Empty {
    char c;
};
 
class Dummy {
    char c;
};
 
int main()
{
    cout << "sizeof(Empty) " << sizeof(Empty) << endl;
    cout << "sizeof(Derived1) " << sizeof(Derived1) << endl;
    cout << "sizeof(Derived2) " << sizeof(Derived2) << endl;
    cout << "sizeof(Derived3) " << sizeof(Derived3) << endl;
    cout << "sizeof(Derived4) " << sizeof(Derived4) << endl;
    cout << "sizeof(Dummy) " << sizeof(Dummy) << endl;
 
    return 0;
}
输出
sizeof(Empty) 1
sizeof(Derived1) 1
sizeof(Derived2) 8
sizeof(Derived3) 1
sizeof(Derived4) 16
sizeof(Dummy) 1



要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”