#include <iostream>
#include <vector>
#include <typeinfo>
using namespace std;
template <class T>
void clearVec ( vector<T> vec)
{
//for(typename vector<T>::iterator it = vec.begin(); it != vec.end(); ) {
//for_each(vec.begin(), vec.end(), delete) {
// delete *it;
//vec.erase(it);
//}
//vec.clear();
}
class A
{
public:
A()
{
cout << "Constructor A\n";
}
virtual ~A()
{
cout << "Destructor A\n";
}
virtual void update()
{
func ();
func2 ();
}
virtual void func()
{
cout << "func A\n";
func2();
}
virtual void func2()
{
cout << "func2 A\n";
}
virtual double foo(unsigned a) const
{
return 2.5;
}
};
class B: public A
{
public:
B():A()
{
cout << "Constructor B\n";
}
virtual ~B()
{
cout << "Destructor B\n";
}
//virtual void func()
//{
// A::func();
// cout << "func B\n";
//}
void func2()
{
cout << "func2 B\n";
}
};
typedef double (A::*Method)(unsigned) const;
void test(A* a, Method mtd){
unsigned b = 5;
cout << "test met " << (a->*mtd)(b) << endl;
}
template<class T>
bool isSameObject(T const * p, T const * q)
{
//T* a = const_cast<T*>(p);
//T* b = const_cast<T*>(q);
//return dynamic_cast<T*>(a) == dynamic_cast<T*>(b);
//return dynamic_cast<const T*>(p) == dynamic_cast<const T*>(q);
return dynamic_cast<T const*>(p) == dynamic_cast<T const*>(q);
}
int main() {
// your code goes here
//B* b = new B();
//A* a = new B();
//b->update();
//a->update();
//delete a;
//delete b;
//vector<A*> v;
//v.push_back(new B());
//v.push_back(new A());
//for(vector<A*>::iterator it = v.begin(); it != v.end(); ++it ) {
//for(size_t it = 0; it < v.size(); ++it ) {
//A* a = v[it];
// A* a = *it;
//delete a;
//}
//clearVec(v);
//v.clear();
A* a = new A();
A* b = new B();
b->func();
cout << b << endl;
//cout << typeid(*(dynamic_cast<void*>(b))).name();
cout << isSameObject(b, b);
test(a, &A::foo);
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8dHlwZWluZm8+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cg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