unions having virtual functions

R

Rahul

Hi Everyone,

I have the following union declarations,

union sample
{
int p;
virtual void fun();
void display(); // accessing value of p...
};

Now assuming that the size of int is 4 bytes and that of a pointer
too is four bytes, would it work as expected?
memory of the union is the same as that of the largest member of the
union...

Thanks in advance!!!
 
J

jkherciueh

Rahul said:
Hi Everyone,

I have the following union declarations,

union sample
{
int p;
virtual void fun();
void display(); // accessing value of p...
};

From the standard [9.5/1]:

... A union can have member functions (including constructors and
destructors), but not virtual (10.3) functions.

The declaration of "sample" is therefore invalid.
Now assuming that the size of int is 4 bytes and that of a pointer
too is four bytes, would it work as expected?

What is expected? There is no pointer in "sample".

memory of the union is the same as that of the largest member of the
union...

From the standard [9.5/1]:

... The size of a union is sufficient to contain the largest of its data
members. ...

As far as I can see, this only gives a lower bound for the size.



Best

Kai-Uwe Bux
 

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
474,184
Messages
2,570,973
Members
47,527
Latest member
RoxanneTos

Latest Threads

Top