भी एक प्रस्ताव पोर्टेबल ABI परिभाषित करने के लिए किया गया था।
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class Base {
public:
virtual void show() {}
};
class Derived : public Base
{ };
class NonVirtual {
public:
void show() {}
};
struct Test
{
int data1, data2;
};
template <typename T>
void showContents(T* obj, string name)
{
Test* test = new Test{};
test = reinterpret_cast<Test*>(obj);
cout << name << ": " << hex << "0x" << test->data1 << " " << "0x" << test->data2 << endl;
delete test;
}
int main()
{
Base* base = new Base{};
Derived* derived = new Derived{};
NonVirtual* nonVirtual = new NonVirtual{};
showContents(base, "Base");
showContents(derived, "Derived");
showContents(nonVirtual, "NonVirtual");
delete base;
delete derived;
delete nonVirtual;
}
Live demo
: मैं इस साधारण प्रोग्राम है कि आधार वर्ग की सामग्री को प्रिंट, व्युत्पन्न वर्ग और एक वर्ग है कि आधार वर्ग के रूप में लेकिन आभासी एक के बजाय एक सामान्य विधि के साथ एक ही है लिखा था
cpp.sh साथ यह संकलन के बाद उपरोक्त कार्यक्रम चलाने का परिणाम (मुझे यकीन है कि क्या संकलक वहाँ प्रयोग किया जाता है नहीं कर रहा हूँ):
Base: 0x4013e0 0x0
Derived: 0x401400 0x0
NonVirtual: 0x0 0x0
इसलिए मुझे उम्मीद है कि Derived
ऑब्जेक्ट के लिए वास्तव में एक वर्चुअल टेबल बनाया गया है (कम से कम इस कंपाइलर के लिए - क्योंकि आवश्यक व्यवहार सी ++ मानक में परिभाषित नहीं है)।
कौन सा कंपाइलर? मानक सी ++ नहीं जानता कि एक vTable क्या है। – Quentin
आप इसकी परवाह क्यों करते हैं? क्या फर्क पड़ता है? –
मैं वर्चुअल टेबल और उसके सभी मामलों के बारे में उत्सुक हूं जहां हमें डायनामिक पॉलिमॉर्फिज्म प्राप्त करने के लिए वर्चुअल टेबल की आवश्यकता है। –