Hi
I have never come across such code before. I can't make out what it is

SockerManager::SocketManager() : sockets(0), events(0), nSockets(0) {}

Could someone tell me what does something like a() : b() mean?

Thanks

Devesh

Recommended Answers

All 3 Replies

The things after the colon (' : ') are the initialisation list. This is a way to pass values to the constructors of member variables of a struct or class. It has essentially the same effect as writing:

SockerManager::SocketManager()
{
   sockets = 0;
   events = 0;
   nSockets = 0;
}

However, if you do this then the variables are first initialised with their default constructor and then assigned the value in the body of the SocketManager constructor. This might be undesirable if default construction is expensive, or if a default constructor and assignment operator do not exist, so in these cases you must use an initialisation list entry. You'd also have to do this if one of the member variables is declared const .

It's an initializer. It's the same as this...

SockerManager::SocketManager()
{
    sockets = 0;
    events = 0;
    nSockets = 0;
}
#include <iostream>
using namespace std;



class SocketManager
{
    private:
        int sockets, events, nSockets;
    public:
        SocketManager();
        void print()
        {
            cout << sockets << '\t' << events << '\t' << nSockets << endl;
        }
};

SocketManager::SocketManager() : sockets(6), events(8), nSockets(9) {}




int main()
{
    SocketManager s;
    s.print();
    return 0;
}

Output...

6       8         9

Thanks a lot ravenous and VernonDozier :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.