Re: Should one use std::string*, std::map* to avoid copying ?
On Jan 8, 5:25 pm, digz <Digvijo...@gmail.com> wrote:
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.
State is not an object, its a type.
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
Nothing wrong with storing pointers. You are asking us if the code is
correct and we can't see how you are storing the pointers nor how you
are using the process function. Personally, i'ld have declared an
abstract State class with 2 derivatives, one for each type (instead of
confusing the setup with one variant). The more you try to confuse the
program by using tags to suggest an object is type A or type B, the
less the compiler can do its job: typechecking.
Its rather difficult to comment on anything else. Except to say that
the code looks rather confused to me.
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;
}
}