D
digz
This is a very simplified version of something I am trying to
understand.
The State object holds the strings and maps , and I pass a reference
to State to the process Function which manipulates it based on the tag
passed in.
I cannot pass the strings/maps in question as references coz ..
a) thats redundant, state has everything, it increases number of
parameters unnecessarily
b) I have to do the case/switch outside the function to decide what to
pass in.( and this is a simple example )
Is it a good idea to use things like string* and std::map..* as i have
tried below
Thanks
Digz
------------------------
#include<string>
#include<map>
using namespace std;
typedef std::map<int, string> mapType;
struct State {
string pId_;
mapType pTypeNSymbol_;
string uId_;
mapType uTypeNSymbol_;
};
void process( State& state, int Tag){
typedef mapType::iterator mItr ;
string* prodId;
mapType* typeSymMap;
switch( Tag ) {
case 0:
prodId = &state.pId_;
typeSymMap = &state.pTypeNSymbol_;
break;
case 1:
prodId = &state.uId_;
typeSymMap = &state.uTypeNSymbol_;
break;
}
if (prodId->empty()) {
mItr it;
mItr end = typeSymMap->end();
if ( (it = typeSymMap->find(6)) != end){
*prodId = (*typeSymMap)[6];
}
}
if (!typeSymMap->empty()) {
mItr it = typeSymMap->begin();
*prodId = it->second;
}
}
understand.
The State object holds the strings and maps , and I pass a reference
to State to the process Function which manipulates it based on the tag
passed in.
I cannot pass the strings/maps in question as references coz ..
a) thats redundant, state has everything, it increases number of
parameters unnecessarily
b) I have to do the case/switch outside the function to decide what to
pass in.( and this is a simple example )
Is it a good idea to use things like string* and std::map..* as i have
tried below
Thanks
Digz
------------------------
#include<string>
#include<map>
using namespace std;
typedef std::map<int, string> mapType;
struct State {
string pId_;
mapType pTypeNSymbol_;
string uId_;
mapType uTypeNSymbol_;
};
void process( State& state, int Tag){
typedef mapType::iterator mItr ;
string* prodId;
mapType* typeSymMap;
switch( Tag ) {
case 0:
prodId = &state.pId_;
typeSymMap = &state.pTypeNSymbol_;
break;
case 1:
prodId = &state.uId_;
typeSymMap = &state.uTypeNSymbol_;
break;
}
if (prodId->empty()) {
mItr it;
mItr end = typeSymMap->end();
if ( (it = typeSymMap->find(6)) != end){
*prodId = (*typeSymMap)[6];
}
}
if (!typeSymMap->empty()) {
mItr it = typeSymMap->begin();
*prodId = it->second;
}
}