Confusing Typedef/Struct, What Does This Do?

J

j6vflbl6vy6g8o001

Sorry if this is really dumb, but I've searched for an example like
this and although can find many structures containing references back
to themselves, none with the additional complexities I see here.

I would really appreciate an explanation of what this is / is doing!
Thanks!

typedef struct SocketError {
int socketErrno;
unsigned short socketPort;
const char *message;
SocketError(const int n, const unsigned short q, const char *a)
{socketErrno = n; socketPort = q; message = a;}
};

And is used thusly:

try {
mySnmp = new SnmpSet(mib, sizeof(mib)/sizeof(MIB_T),
strSnmpHost, strSnmpPort, iSnmpInterval);
}
catch (SocketError *e)
{
cout << "Error connecting to SNMP" << endl;
cout << "errno = " << e->socketErrno << ", Port: " <<
e->socketPort;
cout << ", Message: " << e->message << endl;
return false;
}

Obviously I can sort of see what's going on, but an explanation,
specifically of what the "SocketError(param, param, param) {statement;
statement; statement;}" part is doing within the context of the
structure would really help!

Thanks in advance!
 
G

gottlobfrege

typedef struct SocketError {
int socketErrno;
unsigned short socketPort;
const char *message;
SocketError(const int n, const unsigned short q, const char *a)
{socketErrno = n; socketPort = q; message = a;}
};


Obviously I can sort of see what's going on, but an explanation,
specifically of what the "SocketError(param, param, param) {statement;
statement; statement;}" part is doing within the context of the
structure would really help!

Thanks in advance!

It is a constructor. Just as if SocketError was a class not a struct.
A struct is just a class where all members default to public instead of
private.

The typedef is just a C leftover. Not really needed.
Actually, the typedef is weird - there kinda needs to be a type defined
at the end of the struct, like this:

typedef struct Foo { int x; } Foobar;

ie Foobar is a typedef of struct Foo.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,832
Latest member
UtaHetrick

Latest Threads

Top