0

I am studying a part of c++ code. I have a head file called efacm.h and a corresponding cpp file called efacm.cpp. The efacm.h file is like this:

#include "al.h" 
#include "bl.h"
#include "cl.h"

#include <map>
#include <string>

class efacm
  {
    public:
          efacm();
          virtual ~efacm();
          void init();
          efacm* getcom(std::string acmdstr);

    private:
          typedef std::map<std::string, efacm*> com_map;
          com_map mcom;

          al   al; // the first al is a class name which is defined in al.h
          bl   bl; // same as al  
          cl   cl; // same as al

  }

Then the efacm.cpp is like:

#include "efacm.h"

efacm::efacm()
  : mcom()
  , al()
  , bl()
  , cl()
{}  // I DO NOT understand this part! 
efacm::~efacm() {}
void efacm::init()
 {
   mcom["a"]  = &al;  // I also DO NOT understand this part, why mcom is a vector?
   mcom["b"]  = &bl;
   mcom["c"]  = &cl;
 }

efacm* efacm::getcom(std::string acmdstr)
 {
  com_map::iterator pos=mcom.find(acmdstr);
  return (pos == mcom.end()) ? NULL : pos->second;
 }

As I mentioned in my codes, there are two parts that I do not understand very much. The first one is about

efacm::efacm()
  : mcom()
  , al()
  , bl()
  , cl()
{} 

The structure looks like class::object():#something here# {object body}. But how to interpret this structure? What do the double colon and the single colon mean here?

The second thing I do not understand is:

void efacm::init()
 {
   mcom["a"]  = &al;  
   mcom["b"]  = &bl;
   mcom["c"]  = &cl;
 }

Here why mcom is a vector? It seems that com_map is not a vector type but mcom is an object of type com_map....

Anyone met such kind of things before?

gladys0313
  • 2,569
  • 6
  • 27
  • 51
  • 2
    Did you see this: http://stackoverflow.com/questions/4269034/what-is-the-meaning-of-prepended-double-colon-to-class-name?rq=1 and http://stackoverflow.com/questions/1711990/what-is-this-weird-colon-member-syntax-in-the-constructor? – EdChum Oct 08 '15 at 13:20
  • @gladys0313 What makes you think `mcom` is a vector?! – Biffen Oct 08 '15 at 13:22
  • The first one is called initalization list. mcom is a map. – flowit Oct 08 '15 at 13:22
  • 1
    Note that indexing (`operator[]`) is overloadable like any other operator. There's nothing saying that the left-hand-side of `[]` must be a vector. – unwind Oct 08 '15 at 13:23
  • 2
    Please ask one question per question. Your two question are only related by the code base not the principal. – NathanOliver Oct 08 '15 at 13:23
  • Read about [constructors and member initializers list](http://en.cppreference.com/w/cpp/language/initializer_list) and [`std::map`](http://en.cppreference.com/w/cpp/container/map). – axiac Oct 08 '15 at 13:24
  • @Biffen Oh..just because mcom["a"], mcom["b"]..... – gladys0313 Oct 08 '15 at 13:38
  • @gladys0313 And? Can't non-vectors have an `operator[]`? Are vectors usually indexed by strings? – Biffen Oct 08 '15 at 13:45

0 Answers0