0

I'm newbie in c++, consider the following snippet:

class myClass
{
  ...
  struct EntryKeyBase
  {
     void setOperation(OpType oper)
     {
        operation = oper;
     }
     OpType getOperation() const
     {
        return operation;
     }
     virtual void serialize(std::ostream& os) const = 0;
     protected:
        OpType operation;
    };

  struct ProtoEntryKey: EntryKeyBase
  {
     // some methods here

     ProtoEntryKey(uint8_t l4proto) : proto(l4proto)  // ???
     {
        operation = Inserted;
     }

     protected:
        uint8_t proto;
  };

  // here more structs defined...


  public:
    ...

};

What does the line marked ??? do? I understand that we declare structure inheriting from EntryKeyBase, but whatever follows ':' I don't understand, what does this syntax really mean? Thanks!

Mark
  • 6,052
  • 8
  • 61
  • 129

4 Answers4

3

It is an initialization list. it assigns value of l4proto to proto variable in struct ProtoEntryKey.

Igor Popov
  • 2,588
  • 17
  • 20
1

It is simple constructor. If you write : somefield(somevalue) after constructor it will set value of somefileld to somevalue.

In your example it will set value of proto to l4proto (constructor argument).

Ari
  • 3,101
  • 2
  • 27
  • 49
  • It is not simple. It's also not a constructor. It's a (base/member) initializer list. Which can be a part of a constructor. Yes – sehe Sep 04 '13 at 13:09
  • So it would have the same meaning if I put `proto=l4proto` inside of the body of `ProtoEntryKey` ? – Mark Sep 04 '13 at 13:17
  • @Mark: No. If you put it inside the constructor, the default constructor gets called for each member, and then the copy-assignment will get called. Putting it in an intializer-list makes it so only the copy-constructor gets called for the members in the initializer-list. – Zac Howland Sep 04 '13 at 13:19
0

proto is a unit18_t member. It makes sense to initialise it in the constructor.
The : indicates the initialiser list. You can initialise base classes and member here. See here for further details.

Community
  • 1
  • 1
doctorlove
  • 18,872
  • 2
  • 46
  • 62
0

It's part of the constructor that sets proto variable to l4proto value.

This link may be useful.

Community
  • 1
  • 1
W4lker
  • 51
  • 1
  • 7