0

I am learning to use map containers, and I can see the logic, we can define the key and the value when we insert any object. I also know that we can insert pairs with these containers and can access the content through .first and .second. However I can not understand this piece of code and need someone to enlighten me if you can:

ClassA.h

#include"ClassB"
#include"ClassC"

class A
{
public:
    template<typename T>
    void foo1(classC::ID id);
private:
    ClassB::Ptr someFunction(classC::ID id);
private:
    //map 
    std::map<class::ID, std::function<classB::Ptr()>> mapName;
}

template<typename T>
void ClassA::foo1(classC::ID id)
{
    mapName[id] = [this]()  // <-------------- that [this]() ???
    {                       // Is this calling for that
                            // function<class::Ptr()> inside map??
        //TODO
    }
}

ClassB.h

#include"ClassC"

class B
{
    typedef std::unique_ptr<ClassB> Ptr;
public:
    classB(param1 from other classes , param2 from other classes);
    ....
}

ClassC.h

namespace States
{
    enum ID
    {
        foo,
        foo1,
        ...
    }
 }        

Thank you

Nuno Barão
  • 33
  • 1
  • 6
  • This code is not valid C++. What is `class::ID?` – Neil Kirk Feb 20 '15 at 14:12
  • i name it class as an example, in place you can name it whatever you want, in my example it is not a class it is a struct with a enum so struct::ID if that makes sense to you. – Nuno Barão Feb 20 '15 at 14:15
  • @user3763930 - is `function` a member of some class? – Kiril Kirov Feb 20 '15 at 14:16
  • No i´m not having any errors, i´m actualy learning from a example code from sfml book, but i did not post the exact block of code, but i can change it. So that topic strange use of [] doesn ´t fit on my answer. thank you – Nuno Barão Feb 20 '15 at 14:18
  • Ok i´m going to update my code as i can see that i´m not being clear here. – Nuno Barão Feb 20 '15 at 14:21
  • 1
    @user3763930: The point is, the code you showed us cannot compile because `class` is a keyword. Even if you used something else, like `Example`, it would still not be a compilable piece of code. In other words, the code you showed us cannot be real, and it is impossible to know what code means if we don't see it... – Christian Hackl Feb 20 '15 at 14:21
  • Guys. Is `<----------` not clear enough? So the OP made a mistake to replace his real classname by `class`. Pfft. – sehe Feb 20 '15 at 14:30

1 Answers1

1

In your example, the "strange part" actually has nothing to do with maps and accessors. This this{/*TODO*/} is a C++ lambda expression - a simple function without arguments, with access to "this" pointer.

The usage of map in this example is completely ordinal - you just access element with key [id] (create it if it didn't exist), and then assign the lambda function (value) to this key. If you look at the definition of the map, you can see, that the value type is in fact std::function. More info about this template you can find under http://www.cplusplus.com/reference/functional/function/

Marandil
  • 1,042
  • 1
  • 15
  • 31