Can any one tell me why this happens ?

M

Marvel

/// The header file is:

#include <iostream>
#include <string>
#include <vector>
#include <typeinfo>
#include <ext/hash_map>
#include <string>

#define hashmap __gnu_cxx::hash_map

namespace __gnu_cxx {

template<> struct hash< std::string > {
size_t operator()(const std::string& s) const {
size_t res = 0;
for(size_t i = 0; i < s.size(); ++i) {
res *= 37;
res += s;
}
return res;
}
};

}

template< typename GeneralBaseType >
class TypeCreator {
public:
typedef GeneralBaseType base_type;
typedef base_type* create_function();
typedef std::string string;

class ShowLive : public hashmap< string, create_function* > {
public:
ShowLive() { std::cout << "Create" << std::endl; }
~ShowLive() {std::cout << "Release" << std::endl; }
};

typedef ShowLive MapType;

public:
static base_type* create( const string & tyid) {
std::cout << &map << std::endl;
std::cout << map.size() << std::endl;
typename MapType::const_iterator it = map.find(tyid);
if( it != map.end()) {
return it->second();
}
return NULL;
}

static void release( base_type* p) { delete p; }

static const MapType map;
};

template< typename GeneralBaseType >
const typename TypeCreator< GeneralBaseType >::MapType
TypeCreator< GeneralBaseType >::map;

template< typename TypeToCreate,
typename GeneralBaseType >
static GeneralBaseType*
type_creation_function() {
return new TypeToCreate();
}

template< typename TypeToCreate,
typename GeneralBaseType >
static std::string
static_register_type_to_typecreator() {
typedef TypeCreator< GeneralBaseType > TypeCreator;
typename TypeCreator::string id = typeid( TypeToCreate ).name();
const_cast< typename TypeCreator::MapType&
(TypeCreator::map).insert( typename
TypeCreator::MapType::value_type(id, &type_creation_function<
TypeToCreate, GeneralBaseType > ));
std::cout << &TypeCreator::map << std::endl;
std::cout << TypeCreator::map.size() << std::endl;
return id;
}

template< typename TypeToCreate,
typename GeneralBaseType >
class RegisterTypeCreator :
public TypeCreator< GeneralBaseType > {
public:
typedef typename TypeCreator< GeneralBaseType >::string string;
static const string id;
};

template< typename TypeToCreate,
typename GeneralBaseType >
const typename RegisterTypeCreator< TypeToCreate, GeneralBaseType
RegisterTypeCreator said:

/// The source file is:
#include <iostream>
#include <cassert>
#include <sstream>
#include "header.hpp"

class Base { public: virtual ~Base() {} virtual void p() const = 0; };
class A : public Base { public: void p() const { std::cout << "A" <<
std::endl; } };
class B : public Base { public: void p() const { std::cout << "B" <<
std::endl; } };



int main( int argc, char** argv) {
std::cout << "main" << std::endl;
// Note the following line is important, all it will crash.
//TypeCreator< Base >::map;
Base* p = TypeCreator< Base >::create( RegisterTypeCreator< A,
Base >::id );
//p->p();
TypeCreator said:
//p->p();
TypeCreator< Base >::release(p);
return 0;
}

///

I tried the upper code under g++ version 4.2.4 (Ubuntu 4.2.4-1ubuntu4)

0x804d3b8
1
0x804d3b8
2
Create
main
0x804d3b8
0
0x804d3b8
0
Release

You see that the member function is called before constructor. Why and
how is this happened ?
 
K

Krice

You see that the member function is called before constructor. Why and
how is this happened ?

I'm not sure, but I guess because you are raping C++ with
that code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top