struct understanding

Hello,

I was looking for some examples explaining how to set up a web server and how to use json and I found the following arduino-json-IO

I’m trying to understand how works the code but I have a problem with the following syntax:

(line 368)

struct http_rq {
    EthernetClient &client;
    unsigned long t0;         // used for enforcing timeouts
    int  content_length;
    int  operation;
    char *bufp;
    int bufsiz;
    [b]http_rq(EthernetClient &ec) : client(ec) {}  // reference requires init[/b]
};

(line 1124)

struct http_rq rq(ec);

What does " http_rq(EthernetClient &ec) : client(ec) {} // reference requires init" means?

Thank you

Philippe

References can not be assigned to, they have to be initialized, in this case with an init list.

Thank you Whandall for the answer but I still don't get it. My question is related to the syntax:

http_rq(EthernetClient &ec) : client(ec) {}

Is it a "constructor"? Why do we have : between http_rq(EthernetClient &ec) and client(ec)? client(ec) refers to the Ethernet &client in the structure?

Thank you

Philippe

The : is the start of the initializer list.

http://en.cppreference.com/w/cpp/language/initializer_list

It’s difficult (for me) to understand why this is a struct, and not a class.

AWOL: It's difficult (for me) to understand why this is a struct, and not a class.

To leave all those members as public. Lazy coder didn't want to have to write getters and setters. Or maybe wise coder wanted to save the space.

philippe.noad: ...I have a problem with the following syntax...

In C++ struct and class are essentially interchangeable.

http_rq(EthernetClient &ec) is a method (specifically a constructor) for the http_rq class (aka the http_rq struct).

The difference is that members are public by default in a struct and private by default in a class. So while you can just put "public" in your class and make it the same, it's nice sometimes to let the people using the API know that they can expect public access. And using struct makes that message loud and clear because there's really no other reason to ever use them.

AWOL: It's difficult (for me) to understand why this is a struct, and not a class.

A struct and a class are, basically, the exact same thing, except for a struct all member data is, by default, public, and a struct does not normally contain any methods. Otherwise, they are the same thing, "under the skin".

Regards, Ray L.

RayLivingston:
A struct and a class are, basically, the exact same thing, except for a struct all member data is, by default, public, and a struct does not normally contain any methods. Otherwise, they are the same thing, “under the skin”.

Regards,
Ray L.

I know all that, but what puzzles me is why anyone would want to expose stuff I don’t think you’d really want exposed.