It will be pretty interesting if you love C++ compiler. How it generates code and how it converts class function call into normal function call.
Consider the below simple C++ program
#include iostream.h
class Test
{
Public:
void Print()
};
void Test::Print()
{
cout<<”I am Alive”;
}
void main()
{
Test *ptr = NULL;
ptr->Print();
}
How it works, I didn’t create object for that class Test.
1) Is this right to call a member function without creating object?
2) Can we call all member function without creating object for that?
----Never ever try to call a member function without creating an object for that.It will work correctly, but the function should not be a virtual and it should not use any member variable for manipulating. but it can use static variables of that class. Calling a function without creating object is closely related to static methods of that class.
static method rules applicable to this:-) :-).......
1) Why cannot we call virtual member function as like normal function(without creating object, calling member function)?
Calling virtual function is runtime linking, that is, it has to get the function address from VTABLE. The base address of VTABLE is present in Object's first four bytes. so its mandatory to allocate memory before calling a virtual function. But always try to call all type of member function after creating memory for that .
No comments:
Post a Comment