public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* static map in a base class?
@ 2002-11-01  4:18 Colin Law
  2002-11-01  5:21 ` Eljay Love-Jensen
       [not found] ` <4.3.2.7.2.20021101072826.00b8dde0@iplan-mn.corp.adobe.com>
  0 siblings, 2 replies; 3+ messages in thread
From: Colin Law @ 2002-11-01  4:18 UTC (permalink / raw)
  To: gcc-help

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) ;
}

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: static map in a base class?
  2002-11-01  4:18 static map in a base class? Colin Law
@ 2002-11-01  5:21 ` Eljay Love-Jensen
       [not found] ` <4.3.2.7.2.20021101072826.00b8dde0@iplan-mn.corp.adobe.com>
  1 sibling, 0 replies; 3+ messages in thread
From: Eljay Love-Jensen @ 2002-11-01  5:21 UTC (permalink / raw)
  To: Colin Law, gcc-help

Hi Colin,

A's static theMap will be initialized by the time main is called.

B's static testB will be initialized by the time main is called.  But note, 
it DEPENDS on A's static theMap being initialized BEFORE it gets 
initialized, and MAY BE initialized BEFORE A's static theMap is 
initialized!  There is NO guarantee of order-of-initialization between 
different compile modules.

One way to alleviate the cross-module initialization conundrum is via:
// In a.cpp
class A
{
public:
A() ;
~A() ;
void doSomething() ;
static std::map<int,std::string>& getMap();
};

// In a.h
std::map<int,std::string>& A::getMap() {
static std::map<int,std::string> A::theMap ;
return theMap;
}

The trick works because getMap's static theMap is GUARANTEED to be 
initialized the first time the routine is accessed.  Regardless of 
cross-module order-of-initialization dependencies.

- - - - - - - - - - - -

Some other nits:
You didn't #include <string>.
You didn't #include <utility>, for the pair template.
Your sentinels, e.g. __A_H__, are illegal.  I recommend a_h_ONCE instead.

Sincerely,
--Eljay

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: static map in a base class?
       [not found] ` <4.3.2.7.2.20021101072826.00b8dde0@iplan-mn.corp.adobe.com>
@ 2002-11-01  6:16   ` Colin Law
  0 siblings, 0 replies; 3+ messages in thread
From: Colin Law @ 2002-11-01  6:16 UTC (permalink / raw)
  To: Eljay Love-Jensen; +Cc: gcc-help

Eljay Love-Jensen wrote:

> I may have had some typos in my pseudo-C++-code.  Nothing to hard to 
> touch up.
>
> :-)
>
> --Eljay
>
Eljay,

Thanks for the help, this has been bugging me for a while now.

~Colin.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2002-11-01 14:16 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-11-01  4:18 static map in a base class? Colin Law
2002-11-01  5:21 ` Eljay Love-Jensen
     [not found] ` <4.3.2.7.2.20021101072826.00b8dde0@iplan-mn.corp.adobe.com>
2002-11-01  6:16   ` Colin Law

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).