क्या कोई भी C++ में निम्न कोड के आउटपुट के लिए सटीक कारण बता सकता है? कोड के लिए मुझे प्राप्त आउटपुट हेडर टिप्पणियों में शामिल किया गया है। वर्चुअल टेबल और वी पॉइंटर के साथ इसका क्या संबंध है।खाली कक्षाओं का उपयोग कर वर्चुअल विरासत
/* sizeof(Empty) 1
sizeof(Derived1) 1
sizeof(Derived2) 8
sizeof(Derived3) 1
sizeof(Derived4) 16
sizeof(Dummy) 1
*/
#include <iostream>
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;
}
[आउटपुट पर आउटपुट थोड़ा अलग है] (http://ideone.com/1PZ8b4) [हालांकि, coliru के माध्यम से g ++ आपके साथ सहमत है] (http://coliru.stacked-crooked.com/a/2bc0dc89c3fb717f) – AndyG
यहां देखें http://stackoverflow.com/questions/3972548/virtual-dispatch-implementation- विवरण – user2079303
@ Rndp13: कृपया सब कुछ के लिए अप्रासंगिक और बेकार टैग जोड़ने को रोकें। –