class Class {
public:
Class(int a) /* public constructor */
{
....
}
Class foo() /* function returning a Class object */
{
int bar;
Class ret(bar); /* Here i want call the private constructor */
return ret;
}
private:
Class(int b) /* private constructor */
{
....
}
}
I know this is wrong. I'd like my function foo() to call the private constructor, but i cannot overload it because the arguments are of the same type, although logically they are different entities.
Is there any way to force it? I've tried to put a "typedef int mytipe" in the class, and redefine the private constructor with a "mytipe" argument, but doesn't work.
You cannot have multiple constructors with the same argument list.
What is the point of foo? You are creating a new instance of a class, but you already need an instance of it to begin with? It seems like a really bad way to accomplish this?
your private constructor could be something like
1 2 3 4
Class createClass(int b) {
return Class(b); // Calls public constructor
}
Thanks for the hint, interesting link.
"factory method pattern" describes two way to choose the constructor, or the class.
The first is to have a descriptive constructor... but I think in C++ it's not possible (i think)
The second is unsable by me, because in my constructor I cannot perform any kind of control of input arguments.
The only thing that would have to discriminate which constructor to use, is the side whereby it's called. I mean, the programmer's side, and the internal class side (when it's called in the foo() function)
class Class {
public:
Class(int a) /* public constructor */
{
Class(a, defaultB);
}
Class foo() /* function returning a Class object */
{
int bar;
int a;
Class ret(a, bar); /* Here i want call the private constructor */
return ret;
}
private:
Class(int a, int b) /* private constructor */
{
....
}
}
class Socket {
public:
Socket(int family, int protocol = 0)
{
sd = ::socket(family, protocol)
}
Socket accept()
{
return Socket(sd); /* Here the problem */
}
....
private:
int sd;
Socket(int list_sd)
{
socklen_t len;
struct sockaddr remote;
sd = ::accept(list_sd, &remote, &len);
}
....
}
I don't know whether the concept is clear... there are two ways to create a socket, one with socket() and one with accept(). The second must be accessible only by inside, the first should have to be used only by outside.
I know I could apply some tricks, like putting an extra arbitrary argument in the private constructor, but i'd like to find a general method, and more elegant :)
Probably there's a better way to implement my class, instead of using constructor overloading?
Because in network programming, i can do something so (it's an example of pseudoserver):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Socket serversocket();
/* binds the socket... */
serversocket.bind(INADDR_ANY, /* to a local address (every IP bound to any network interface) */
80) /* to a local port (this is an http server) */
serversocket.listen(5); /* accepts max five connections */
while(1) {
/* here we accept the request of a client, and we open another socket to communicate with him*/
Socket connected_socket = serversocket.accept();
/* the communication is handled by a parallel thread */
thread_communicate(connected_socket);
}
serversocket.close();
This is the client side
1 2 3 4 5 6 7
Socket clientsocket();
clientsocket.connect("11.22.33.44", /* server address */
80) /* server port */
do_communication(clientsocket);
clientsocket.close();