Re: So it has been proven
"Marcel M?ller" <news.5.maazl@spamgourmet.org> wrote in message
news:4d6d545d$0$6980$9b4e6d93@newsspool4.arcor-online.net...
Paul wrote:
So it has been proven that, in C++, a nonstatic member function is indeed
different from an ordinary function in more fundamental ways than simply
calling syntax.
In the C++ language: yes.
In common implementations: no.
But the handling of virtual functions and member function pointers adds a
new dimension of complexity. This does not apply to the implementation of
a member function itself, but to the implementation of member function
pointers. So sizeof(MemberFunctionPointer) is in general larger or same
than sizeof(FunctionPointer).
The calling convention is usually the same for void A::foo() and
void bar(A*). However, dereferencing void (A::*)() and void (*)(A*) is in
general a different story, since the really called method also depends on
the object type used to dereference the member function pointer.
Seems like a fair assessment.
Also note that (A::*)() can only call itself with objects of type A or in a
derived heirarchy containing A. (*)(A*) does not have this restraint as the
pointer parameter can theoretically point to any type of object. Thus the
possibility of a different sequence of execution arises.
Example , the following ordinary_function cannot be done with (A::*)():
#include <iostream>
class Animal{public:
virtual void eat(){std::cout<< "Animal Eating"<< std::endl;}
virtual int getID()=0;
static int count;
};
class Dog: public Animal{
public:
void eat(){std::cout<< "Dog Eating"<< std::endl;}
int getID(){return 1;}
};
class Cat: public Animal{
public:
void eat(){std::cout<< "Cat Eating"<< std::endl;}
int getID(){return 0;}
};
int Animal::count =10;
Dog* overwriteCat(Animal* ptr){
delete ptr;
Dog* p = reinterpret_cast<Dog*>(ptr);
p = new Dog;
return p;
}
Cat* overwriteDog(Animal* ptr){
delete ptr;
Cat* p = reinterpret_cast<Cat*>(ptr);
p = new Cat;
return p;
}
void ordinary_function(Animal* obj){
Animal::count--;
std::cout<<"Address of obj: " <<obj << " ";
obj->eat();
if(obj->getID()){overwriteDog(obj);}
else {overwriteCat(obj);}
if(Animal::count){
ordinary_function(obj);
}
}
int main()
{
Cat* p_cat = new Cat;
Animal* p_anim = p_cat;
ordinary_function(p_cat);
}