本文介绍了使用STL列表和映射对象作为静态成员变量的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个小的解析器对象。我需要在

lsts中存储关键字等。因为这个数据是由我的解析器

类的所有实例共享的,所以我已经将变量声明为类变量(即静态)。


//解析器类中的声明(私有部分)

静态列表< stringm_keywords,m_symbols_used;

静态地图< string,myParser :: FuncDatam_mapped_funcs;


我有这样的初始化代码:


m_symbols_used.clear();


//加载关键字

m_keywords.clear()

m_keywords.push_back(ABC);

m_keywords.push_back(CDE);


。 ..


我显然不能将它放在构造函数中,因为它们是静态的 -

(实际上,我试过但我有连锁错误)。关于如何
初始化这些变量的任何想法?

I am writing a small parser object. I need to store keywords etc in
lsts. Because this data is to be shared by all instances of my parser
class, I have declared the variable as class variables (i.e. statics).

//declarations in parser class (private section)
static list<stringm_keywords, m_symbols_used;
static map<string, myParser::FuncDatam_mapped_funcs ;

I have initialization code like this:

m_symbols_used.clear();

//loadup keywords
m_keywords.clear()
m_keywords.push_back(ABC) ;
m_keywords.push_back(CDE) ;

...

I obviously can''t place this in the constructor since they are statics -
(actually, I tried but I had linkage errors). Any ideas as to how to
initialize these variales ?

推荐答案



你通常做的是有一个'int'类型的全局虚拟变量或任何

other,并从一个初始化它功能,你将把你的

全班成员操纵。


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问

What you usually do is have a global dummy variable of type ''int'' or any
other, and initialise it from a function, inside which you will put your
class-wide member manipulation.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask




模拟静态构造函数的一种简单方法是:


A类{

类InitClass {

InitClass();

};

静态InitiClass initiclass;

朋友类InitClass;

};


A:InitClass A:initiclass;


A :: InitClass :: InitClass()

{

//无论如何你不需要初始化静态A

}


有些冗长,但是无需其他数据成员。你可以在InitClass中加入一些

静态成员来使代码更清晰。


-

Salu2

A simple way to simulate an static constructor is:

class A {
class InitClass {
InitClass ();
};
static InitiClass initiclass;
friend class InitClass;
};

A:InitClass A:initiclass;

A::InitClass::InitClass ()
{
// Anyway you need no initialize statics of A
}

Some verbosity, but no additional data member required. And you can put some
static members inside InitClass to make the code cleaner.

--
Salu2




嗯,从我到目前为止的反馈来判断,我'可能会让我的

自我清晰:


这是我课程的(删节版)。


class MyParser

{

public:

MyParser();

MyParser(const string);

MyParser(const MyParser&);

MyParser& operator =(const MyParser&);


private:

typedef struct {string name,int argc} FuncData;


静态列表< stringm_keywords;

静态地图< string,FuncDatam_mapped_funcs;

};


我想知道如何我可以初始化(即填充)静态(和

私有)成员变量吗?


Hmmm, judging by the feedback I''ve had so far, I''d probably make my
self clearer:

This is an (abridged version of) my class.

class MyParser
{
public:
MyParser();
MyParser(const string);
MyParser(const MyParser&);
MyParser& operator= (const MyParser&);

private:
typedef struct { string name, int argc } FuncData ;

static list<stringm_keywords ;
static map<string,FuncDatam_mapped_funcs ;
};

I wanted to know how can I initialize (i.e. populate) the static (and
private) member variables ?


这篇关于使用STL列表和映射对象作为静态成员变量的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 16:20