0

In this https://github.com/pocoproject/poco/blob/dcdcee5afab6e8972f1b144ef7e7e884f85e83a3/Net/samples/Ping/src/Ping.cpp sample link, the constructor is defined as

public:
    Ping(): 
        _helpRequested(false), 
        _icmpClient(IPAddress::IPv4),
        _repetitions(4), 
        _target("localhost")
    {
    }

May i know what are these _variables called? I have never seen constructors defined like this before.

Silent
  • 52
  • 6
  • those are member variables of the class. – NathanOliver May 06 '22 at 16:51
  • 1
    What you're seeing is an application of one of C++'s most useful and least taught features: [The Member Initializer List](https://en.cppreference.com/w/cpp/language/constructor) – user4581301 May 06 '22 at 16:53
  • This might be helpful reading: https://stackoverflow.com/questions/1711990/what-is-this-weird-colon-member-syntax-in-the-constructor – NathanOliver May 06 '22 at 16:53
  • 1
    TL;DR version: All member variables and base classes must be initialized before entering the body of the constructor. The Member Initializer List allows you to perform this initialization when default initialization is not good enough or unavailable. – user4581301 May 06 '22 at 16:56
  • 3
    They are variables. The author has decided to put _ in the name, because. – user253751 May 06 '22 at 16:59
  • 1
    _"I have never seen constructors defined like this before."_ That's a pity, since it's the idiomatic way to do member variable initalizations of a class. @Silent – πάντα ῥεῖ May 06 '22 at 17:27

0 Answers0