Static object initialization issue..

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vjn
    New Member
    • Feb 2008
    • 1

    Static object initialization issue..

    Hi Everybody,
    I have the following class structure. When compiled with arm compiler the code works fine but when I try to run the same in GCC4.1.2 i am getting segment fault. On analysis I found that the constructor of B is called first.. ie. even before calling the constructor for the static object named 'trouble1' in the below code.

    ---------A.hpp-------------------------
    class A;
    class A
    {
    public :
    int val;
    std::string name;
    A(int i, string str);
    A(A a);
    static const A trouble1;
    int val();
    std::string getName();
    -----
    ----
    }

    ---------A.cpp----------------------------

    const A A::trouble1(1," str1")

    int val(){return val;}
    std::string A::getName{retu rn name;}

    A(int i, string str ) : val(i), name(str){}

    A(A a): val(a.val()), name(a.getName( )){}

    -----------B.hpp-------------

    #include "A.hpp"

    class B : public A
    {
    public :
    B() ;
    ---
    ----
    }

    -------------B.cpp-----------

    B::B():A(troubl e1){}


    Both this files are in a single libraray module. While loading the library my application crashes.

    I feel this is the compiler dependent behaviour. Is there any way to make it running with GCC4.1.2 and g++4.1.2.

    Any help or suggestion is appreciated..

    thanks
    vjn
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Read this: http://www.thescripts.com/forum/thread737451.html.

    And post again if you are still having problems.

    Comment

    Working...