473,394 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

static const map

Dear all,

I have found a turnaround solution to the problem of creating a static const map for those who want to make large lookup tables.

Form a class and make the map an instance member of that class. Fill the map in the class' constructor.

After that, make an static instance of that class.

In this way the data is filled only once. And you may define a method to return a value from the map.
Mar 23 '09 #1
6 8734
weaknessforcats
9,208 Expert Mod 8TB
Actually, you do not wnat a static instance of the map<>.

What you need is a Singleton class that manages the map<>.

Read this: http://bytes.com/topic/c/insights/65...erns-singleton.
Mar 23 '09 #2
Yes. I have used a singleton.
Here is the implementation:

ObjectMap.h file:

#include <string>
#include <map>
using namespace std;

class ObjectMap
{
public:
static ObjectMap* GetInstance();
string GetName(unsigned int number) {return mapInstance->m_map[number];}

protected:
ObjectMap();
map<unsigned int, string> m_map;

private:
static ObjectMap* mapInstance;
};
ObjectMap.cpp file:
#include "ObjectMap.h"

ObjectMap* ObjectMap::mapInstance = NULL;

//_________________________________________________
ObjectMap* ObjectMap::GetInstance()
{
if (mapInstance == NULL)
mapInstance = new ObjectMap;
return mapInstance;
}

//_________________________________________________
ObjectMap::ObjectMap()
{
m_map[0] = "Hello World0";
m_map[1] = "Hello World1";
m_map[2] = "Hello World2";
}

You may then create an instance of the ObjectMap class using the new statement and don't delete the pointer so that it will be available all the time and won't need to re-create it.

#include ObjectMap.h

void main()
{
ObjectMap* c = new ObjectMap;
c->GetName(2);
}
Mar 24 '09 #3
weaknessforcats
9,208 Expert Mod 8TB
ObjectMap is not a singleton because you can create many instances of ObjectMap. Each of these instances will compete for your static map.

A Singleton is a class that can have only one instance.

Again, I say, the map should not be static. It should be just a regular member variable. ObjectMap needs to be set up so that only one instance can be created and that way you have one map.

You have no Instance() method to get a pointer to the Singleton.

Read this and pay special attention to a Singleton registry: http://bytes.com/topic/c/insights/65...erns-singleton.
Mar 24 '09 #4
Yes, you are right considering the point that there is no need to form a static instance of the class.

You have no Instance() method to get a pointer to the Singleton.
However, the class I have formed is a singleton. I have a GetInstance() method which is the Instance() method you are looking for.
Mar 24 '09 #5
I am sorry, this part is wrong:
#include ObjectMap.h

void main()
{
ObjectMap* c = new ObjectMap;
c->GetName(2);
}

It should be:

#include ObjectMap.h

void main()
{
ObjectMap* c = ObjectMap::GetInstance();
c->GetName(2);
}
I wanted to edit the original post but I couldn't.
Mar 24 '09 #6
weaknessforcats
9,208 Expert Mod 8TB
That being the case, the map need not be static since there is only one ObjectMap object.
Mar 25 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Jan13 | last post by:
Hi, I'm new to programming in C++ (using VC6) and ran into the following problem: I want to declare and define a class member variable as 'static const', but something seems to go wrong with...
3
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming...
3
by: Steven T. Hatton | last post by:
Sorry about the big code dump. I tried to get it down to the minimum required to demonstrate the problem. Although this is all done with GNU, I believe the problem I'm having may be more general. ...
4
by: cppsks | last post by:
"Defining static const variables inside the class is not universally supported yet, so for now I guess you'll have to move the definition out of the body of the class. No, static const inside...
14
by: Mike Hewson | last post by:
Have been researching as to why: <example 1> class ABC { static const float some_float = 3.3f; }; <end example 1>
9
by: cppsks | last post by:
Taking the address of a static const resulted in a unresolved symbol. Why is that? Is the address assigned at load time? Thanks.
10
by: Dave | last post by:
const static int ARRAY_SIZE = 4; Comeau online gives this warning: "ComeauTest.c", line 10: warning: storage class is not first const static int ARRAY_SIZE = 4; Why is static const...
2
by: Drew McCormack | last post by:
I am getting an error in g++ 4.0.0 that I did not get in g++ 3.4. I have a header with the following const variables with namespace scope: namespace Periphery { extern const double...
5
by: John Goche | last post by:
Hello, I would like to know whethere there is a difference between a const variable and a static const variable inside a class. After all, if a variable is const in a class, the compiler can...
7
by: Spoon | last post by:
Hello everyone, I have a Packet class I use to send packets over the Internet. All the packets sent in a session are supposed to share a common random ID. I figured I'd use a static const...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.