polymorphic class have diferent size on different os

M

mangesh

class B
{
public :
void f() = 0 ;
};

class D : public B
{
public :
void f()
{

}
};

int main()
{
D d ;
int s = sizeof(d);
return 0 ;
}

now value of s will be different on windows and unix , why so ?
 
I

Ian Collins

mangesh said:
class B
{
public :
void f() = 0 ;

This isn't valid, virtual keyword is missing.
};

class D : public B
{
public :
void f()
{

}
};

int main()
{
D d ;
int s = sizeof(d);
return 0 ;
}

now value of s will be different on windows and unix , why so ?
Who says they should be the same?
 
S

Salt_Peter

mangesh said:
class B
{
public :
void f() = 0 ;

virtual void f() = 0;
};

class D : public B
{
public :
void f()
{

}
};

int main()
{
D d ;
int s = sizeof(d);
return 0 ;
}

now value of s will be different on windows and unix , why so ?

Unix is a 64 bit OS and your windows is probably 32 bit.
Regardless, a real programmer must not write code that depends on the
platform beneath.
Hence: what you see is expected and inconsequential.
 
R

Ron Natalie

mangesh said:
now value of s will be different on windows and unix , why so ?

There's not even any guarantee that s will be the same value on
different implementations within UNIX or Windows.
 
F

Frederick Gotham

mangesh posted:

now value of s will be different on windows and unix , why so ?


Three reasons:


(1) The amount of bytes consumed by primitive types is implementation-
specific.
(2) The amount and placement of padding within an object is
implementation-specific.
(3) The way in which virtual functions are implemented is
implementation-specific.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top