This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

static map in a base class?


Hi,

Could somebody explain why the code below will compile, but causes a seg fault when run. It appears the the map is not initialized with the base class when creating a derived class from it... is this correct? Should the map be getting initialized with the base class or am I missing something here?

Suggestions anyone?

Regards,
~Colin.


//A.h
#ifndef __A_H__
#define __A_H__
#include <map>

class A
{
public:
A() ;
~A() ;
void doSomething() ;
static std::map<int,std::string> theMap ;
};
#endif
-------------------------------------------------------
//B.h
#ifndef __B_H__
#define __B_H__
#include <map>
#include "A.h"

class B : public A
{
public:
static B testB ;
B() ;
~B() ;
void doMore() ;
};
#endif
-----------------------------------------------------------
//A.cc
#include "A.h"
#include <iostream>
std::map<int,std::string> A::theMap ;

A::A()
{
std::cout << "A :: Constructor" << std::endl ;
std::cout << "Mapsize = " << theMap.size() << std::endl ;
theMap.insert(std::pair<int,std::string>(1,"test")) ;
std::cout << "Mapsize = " << theMap.size() << std::endl ;
}

A::~A()
{
std::cout << "A :: Destructor" << std::endl ;
}

void
A::doSomething()
{
std::cout << "Do Something" << std::endl ;
}
-----------------------------------------------------------
//B.cc
#include "B.h"
#include <iostream>
B B::testB ;

B::B()
{
std::cout << "B :: Constructor" << std::endl ;
}

B::~B()
{
std::cout << "B :: Destructor" << std::endl ;
}

void
B::doMore()
{
std::cout << "Do More" << std::endl ;
}
-----------------------------------------------------------
//test.cc
#include "A.h"
#include "B.h"
#include <iostream>

int main()
{
std::cout << "test" << std::endl ;

// B b ;
return(0) ;
}


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]