Can any one tell me why this happens ?

Discussion in 'C++' started by Marvel, Apr 26, 2010.

  1. Marvel

    Marvel Guest

    /// 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
    >::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 ?
     
    Marvel, Apr 26, 2010
    #1
    1. Advertising

  2. Marvel

    Krice Guest

    On 26 huhti, 11:00, Marvel <> 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.
     
    Krice, Apr 26, 2010
    #2
    1. Advertising

  3. On 26 avr, 10:00, Marvel <> 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-lite/ctors.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
     
    Michael Doubez, Apr 26, 2010
    #3
  4. Marvel

    Balog Pal Guest

    "Michael Doubez" <>
    > On 26 avr, 10:00, Marvel <> 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-lite/ctors.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.
     
    Balog Pal, Apr 26, 2010
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brahmam
    Replies:
    1
    Views:
    383
    Scott M.
    Mar 28, 2006
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,036
    Smokey Grindel
    Dec 2, 2006
  3. NM
    Replies:
    6
    Views:
    473
    Default User
    Sep 20, 2006
  4. yogesh
    Replies:
    1
    Views:
    372
    Victor Bazarov
    Mar 14, 2007
  5. Replies:
    1
    Views:
    384
    MikeS
    Aug 27, 2006
Loading...

Share This Page