Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Can any one tell me why this happens ?

Reply
Thread Tools

Can any one tell me why this happens ?

 
 
Marvel
Guest
Posts: n/a
 
      04-26-2010
/// 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[i];
}
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
>::string

RegisterTypeCreator< TypeToCreate, GeneralBaseType >::id =
static_register_type_to_typecreator< TypeToCreate, GeneralBaseType
>();


/// 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< Base >::release(p);
p = TypeCreator< Base >::create( RegisterTypeCreator< B, Base
>::id );

//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 ?
 
Reply With Quote
 
 
 
 
Krice
Guest
Posts: n/a
 
      04-26-2010
On 26 huhti, 11:00, Marvel <(E-Mail Removed)> wrote:
> 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.
 
Reply With Quote
 
 
 
 
Michael Doubez
Guest
Posts: n/a
 
      04-26-2010
On 26 avr, 10:00, Marvel <(E-Mail Removed)> wrote:
[snip]
> You see that the member function is called before constructor. Why and
> how is this happened ?


I guess it is a case of static initialization order fiasco ?
http://www.parashift.com/c++-faq-lit...html#faq-10.12

I wouldn't have expected it with only one translation unit but it
seems the template mechanism produces the same effect.

--
Michael
 
Reply With Quote
 
Balog Pal
Guest
Posts: n/a
 
      04-26-2010

"Michael Doubez" <(E-Mail Removed)>
> On 26 avr, 10:00, Marvel <(E-Mail Removed)> wrote:
> [snip]
>> You see that the member function is called before constructor. Why and
>> how is this happened ?

>
> I guess it is a case of static initialization order fiasco ?
> http://www.parashift.com/c++-faq-lit...html#faq-10.12
>
> I wouldn't have expected it with only one translation unit but it
> seems the template mechanism produces the same effect.


With VS2008 teh output looks correct. But indeed there are multiple static
objects (map, id, id) and I don't see an ordering constraint.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
CAN any one tell it whats the code tell it yogesh C++ 1 03-14-2007 01:12 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
501 PIX "deny any any" "allow any any" Any Anybody? Networking Student Cisco 4 11-16-2006 10:40 PM
What happens when type conversion between signed and unsigned happens? NM C++ 6 09-20-2006 05:39 PM
Can someone tell me why I can't delete this file? and why it blue screens WinXP Pro on delete? zZz Computer Support 1 01-12-2005 02:37 AM



Advertisments