-2

When we use sizeof operator on object of that class which do not have any data members. Then what would be the output? Why this always output 1??

class Abhi
{
public :
int sum (int, int);
int avg (int);
};
int Abhi:: sum(int a, int b)
{
float c;
c=a+b;
return 0;
}

int main()
{
Abhi abh;
int c;
c= sizeof(abh);
cout<<c;
}
Abhishek Jain
  • 356
  • 1
  • 3
  • 15

1 Answers1

4

According to the standard, each instance of a class must have a unique address. This is usually implemented by giving an "empty" class (no data members) a minimum size, as if there is a dummy char inside. This is why sizeof gives 1 and not 0.

Neil Kirk
  • 21,327
  • 9
  • 53
  • 91