Autor Beitrag
Aya
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1964
Erhaltene Danke: 15

MacOSX 10.6.7
Xcode / C++
BeitragVerfasst: Mo 26.01.09 21:58 
Hi,

ich hab ein C++ problem und hoffe mir kann hier jemand helfen.

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
class CITestBase {
public:    
  virtual void doSomething();
};

class CITest : public CITestBase {
private:
  float data[4];
};


Wenn ich hier mir von CITest die size ausgeben lasse, kommt 20 raus:

ausblenden C#-Quelltext
1:
cout << sizeof(CITest) << endl; // Output: 20					


allerdings sollte es ja eigentlich 16 sein, also 4 * float = 4 * 4 = 16

Wenn ich bei der CITestBase das "virtual" von der doSomething() funktion wegmache stimmt die größe mit 16byte.

Das Problem ist jetzt, ich brauche die CITest-Klasse mit einer größe von 16byte (wird als array dann an eine andere funktion übergeben die kein padding etc unterstüzt) und brauche aber auch die virtual-funktion.. kann ich da irgendwas tun oder muß ich mir nen workarround ausdenken? :/

Aya

PS: Auch das ganze mit #pragma pack(1) drumrum hift nichts..

_________________
Aya
I aim for my endless dreams and I know they will come true!
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Mo 26.01.09 22:20 
Die 4 Byte für den VMT Pointer wirst du wohl nicht herausbekommen. Dabei ist es egal, ob die virtuelle Methode in der Basisklasse drin steht oder in der abgeleiteten. Auch ist es egal, ob noch weitere hinzukommen, die 4 Byte bleiben AFAIK gleich.

// EDIT:
Hier steht dazu noch mehr:
www.cprogramming.com...of_class_object.html