Re: initialize reference
"jean" <jean.shu@gmail.com> schrieb im Newsbeitrag
news:1153777669.706631.247520@75g2000cwc.googlegroups.com...
Hi,
I have the following code. Basically, I want to insert to different map
depends on m_bDefaultTag . I know I need to initialize updateTags. But
how? Is there a way to achieve my goal? Thanks.
class tag_entry
{
string m_name;
int value;
}
void MyFunc()
{
std::map<std::string, tag_entry >& customTags =
GetCustomTags();
std::map<std::string, tag_entry >& defineTags =
GetDefineTags();
std::map<std::string, tag_entry >& updateTags;
if( m_bDefaultTag ) {
updateTags = defineTags;
}
else {
updateTags = customTags;
}
customTags.insert( mapElement );
}
Use the ternary operator:
void MyFunc()
{
std::map<std::string, tag_entry >& customTags =
GetCustomTags();
std::map<std::string, tag_entry >& defineTags =
GetDefineTags();
std::map<std::string, tag_entry >& updateTags =
m_bDefaultTag ? defineTags : customTags;
// Note: Inserting to updateTags
// rather than customTags.
updateTags.insert( mapElement );
}
As you can see, I also changed the last line of code in MyFunc(). I
guess that this is what you actually mean.
--
Matthias Hofmann
Anvil-Soft, CEO
http://www.anvil-soft.com - The Creators of Toilet Tycoon
http://www.anvil-soft.de - Die Macher des Klomanagers
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]