... (*_employeePayroll)["Karl"]->employeeID ...
NOTE: This works, but is dangerous! It will crash the programm as soon as the key "Karl" doesn't exist. Please, find the last code example below.
The safe way, using find and an iterator:
...
itEmployeeID = _employeePayroll->find("Karl");
if ( itEmployeeID != _employeePayroll->end() )
{
... (itEmployeeID->second)->employeeID ...
The complete test code is here:
#include <iostream>
#include <string>
#include <map>
class Employee
{
public:
int employeeID;
Employee()
{
employeeID = 123;
}
};
int main(int argc, char* argv[]) {
std::map<std::string, Employee *> *_employeePayroll;
std::map<std::string, Employee *> _employeeID;
std::map<std::string, Employee *>::const_iterator itEmployeeID;
_employeePayroll = &_employeeID;
(*_employeePayroll)["Karl"] = new Employee;
itEmployeeID = _employeePayroll->find("Karl");
if ( itEmployeeID != _employeePayroll->end() )
{
std::cout << (itEmployeeID->second)->employeeID;
std::cout << std::endl;
}
return 0;
}
NOTE: The allocated memory has to be cleand up.
The complete test code of the "dangerous" variant is:
#include <iostream>
#include <string>
#include <map>
class Employee
{
public:
int employeeID;
Employee()
{
employeeID = 123;
}
};
int main(int argc, char* argv[]) {
std::map<std::string, Employee *> *_employeePayroll;
std::map<std::string, Employee *> _employeeID;
_employeePayroll = &_employeeID;
int iValue;
(*_employeePayroll)["Karl"] = new Employee;
iValue = (*_employeePayroll)["Karl"]->employeeID;
std::cout << iValue;
std::cout << std::endl;
return 0;
}
NOTE: The allocated memory has to be cleand up.