-3

So I was playing with C++ features and I found that there is a big difference between C-style function pointers and C++ methods. Here is what do I mean:

typedef struct _myStruct {
    void (*myFunctionOne)();
    void(*myFunctionTwo)();
    void(*myFunctionThree)();
} myStruct;

Size is 3 * 4 = 12 bytes.

typedef struct _emptyStruct {
} emptyStruct;

Size is 1 byte.

class myClass {
     void myMethodOne();
     void myMethodTwo();
     void myMethodThree();
};

Size is 1 byte, like this is an empty struct.

Why is it empty? If C++ methods are not function pointers, how do they are implemented? How does class "know" about it's methods, if it doesn't have any pointers? This question doesn't actually distrubs me a lot, but it arouses my curiosity.

Frank C.
  • 7,758
  • 4
  • 35
  • 45
  • 1
    Member functions work the same way regular old free functions. You don't need a function pointer to call those either. – François Andrieux Feb 16 '18 at 19:38
  • 1
    Non-virtual member functions are just functions in a scope -- no pointers in the instance. Virtual functions will not have a pointer for each function in the instance either, just one vtable pointer. – Fred Larson Feb 16 '18 at 19:38
  • 2
    This is a **big** subject. You might be better served getting a [nice C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) that would explain this entire side of C++ – NathanOliver Feb 16 '18 at 19:38
  • 1
    On an unrelated note, the size of you `_myStruct` structure isn't guaranteed to be 12 bytes. It can be more (or less, though that's not very likely). – Some programmer dude Feb 16 '18 at 19:39
  • 1
    Try declaring the methods as virtual and run your test again. – stark Feb 16 '18 at 19:41
  • Keep in mind that the function pointers used in the c `struct` serve a different purpose then non-`virtual` member functions in c++. You can approximate it with `virtual` member functions, which is likely to increase the size of your type slightly. The most common implementation adds a single pointer to a shared table of function pointers. – François Andrieux Feb 16 '18 at 19:46

2 Answers2

4

In the structure with pointers to functions, each pointer is a member variable. The C++ class doesn't have any member variables, it is, as you say, empty.

Member function are not the same as member variables.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
2

Member functions are functions. They are implemented as functions. They are not data and are not stored inside the structure. They are just declared there.

If you have

class myClass {
     void myMethodOne();
     void myMethodTwo();
     void myMethodThree();
};

and then another class

class myOtherClass {
     void myMethodOne();
     void myMethodFortyTwo();
};

then myMethodOne from myClass and myOtherClass don't conflict, because they actually have different names: myClass::myMethodOne and myOtherClass::myMethodOne.

myClass myVar;
myVar.myMethodOne();

This knows to call myClass::myMethodOne and not myOtherClass::myMethodOne because of the type of myVar. It is myClass, so the method of myClass is called.

Virtual functions work differently: they are stored in each object, though usually indirectly (often as a pointer to an entire bunch of functions pointers), This way the size of each object doesn't grow as we add more virtual functions.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
  • I would change the wording "Virtual functions are stored in each object". It is VERY misleading, especially since you proceed to describe the correct implementation (pointer to vtable) immediately after. – Gonen I Feb 16 '18 at 22:05
  • @GonenI Any implementation is correct as long as it's according with the standard. Vtables is one implementation, there are others. – n. m. could be an AI Feb 16 '18 at 22:32