How much to do in a contructor
I am very new to C++ and am trying to learn how most people do things.
I have made a class and am wondering how much is too much in a constructor and what is considered bad practice. What I want to do with this class is (just for now) create a socket ready to listen on a server.
Here is how I am currently doing it.
Code:
class server_socket
{
public:
server_socket(short int port)
{
fport = port;
fsock = 0;
memset(&faddr, 0, sizeof(struct sockaddr_in));
/* Create the socket */
create();
if (!isvalid()){return;}
bind(fport);
if (!isvalid()){return;}
listen();
if (!isvalid()){return;}
//After I create the class I also call isvalid() in the function as well
}
~server_socket()
{
close(fsock);
}
bool isvalid(void)
{
return (fsock != -1);
}
bool send(const char *);
int recv(char *);
private:
int fsock;
short int fport;
struct sockaddr_in faddr;
bool create(void);
bool bind(const int port);
bool listen(void);
bool accept(int *fsock);
};
I also don't know if it is typically a good idea to "return" out of a constructor.
I'm all ears right now because I am trying to write code a standard C++ programmer typically would. Moving from C to C++ has quite a few differences to say the least. Still haven't moved into <string> yet.