0

So using static keyword below we can call MyMethod without creating an object of the class MyClass, my question is how is it even possible as a class is not allocated memory when the code is compiled?

static int MyMethod( int * a, int * b );

int one = 1;
int two = 2;

MyClass::MyMethod( &two, &one );

1 Answers1

0

It seems you have a bit of a misunderstanding. There is no memory allocated for a class's methods every time it is made. Class methods are generally separate from their classes. I guess you could call the "Memory allocated to the member functions" the space that the opcodes take up. But yes, this is separate from the objects. It is just a language requirement that a member function that isn't static must be called by an instance of the class. Static functions intentionally don't care about this, and can be called even when it is not called by an object.

mediocrevegetable1
  • 4,086
  • 1
  • 11
  • 33