3

In the example below, I want to access the employeeID from class Employee by using the pointer employeePayroll:

class Employee { ... int employeeID; ... }
std::map<std::string, Employee *> *_employeePayroll;
std::map<std::string, Employee *> _employeeID;
_employeePayroll = &_employeeID;

How can I access employeeID with a given key, e.g. to print the content?

Jörg Brüggmann
  • 603
  • 7
  • 19
user2537246
  • 143
  • 2
  • 10
  • Possible duplicate of [What does "dereferencing" a pointer mean?](https://stackoverflow.com/questions/4955198/what-does-dereferencing-a-pointer-mean) – francesco Dec 13 '18 at 05:35
  • 1
    I think you have some compile errors there. – TrebledJ Dec 13 '18 at 05:44
  • 1
    Were you intending on finding a specific employee to gather their id? There is no `employeeID` member of `std::map`, so `employeePayroll->employeeID` is nonsense. And `static_cast_employeeID` is equally nonsense. Iit is missing the parens, and even if they were there, `employeeID` isn't a pointer-type, so static casting to `void*` is still nonsense. Remove the "quick" from your "quick question", formulate *exactly* what you're trying to do, the conditions you're trying to do it with, the inputs, the expected outputs, etc, and update your post. – WhozCraig Dec 13 '18 at 05:56
  • Thanks WhozCraig, i changed it. Some typo errors and explain again. – user2537246 Dec 13 '18 at 06:04
  • what you really want? do you get error or something? – apple apple Dec 13 '18 at 06:30
  • 1
    Something like `(*_employeePayroll)["name"]->employeeID` I guess is what you want, although it's hard to really know. – Jonathan Potter Dec 13 '18 at 06:30
  • What I am getting is the memory address if I do cout << &_employeeID; instead of the variable value. But Jonathan Potter is giving me the right idea now. – user2537246 Dec 13 '18 at 06:43

1 Answers1

3
... (*_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.

Jörg Brüggmann
  • 603
  • 7
  • 19