Forcing overloading

I have this example class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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.

Thanks
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

}


Last edited on
closed account (z05DSL3A)
Have a read of the Factory method pattern for the function calling constructor side of things
http://en.wikipedia.org/wiki/Factory_method_pattern
Last edited on
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)
Last edited on
Your constructors are both going to do the same things correct?
You can call 1 constructor from another, but they must have separate argument lists.

In theory there should be no need for multiple constructors with the same parameter list. In your situation my code would look like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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 */
    {
        ....
    }
}


Your constructors are both going to do the same things correct?

No:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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?
Last edited on
Your trying to achieve both client and server functionality within one class?

Ok. I am going to assume you are. This is a bad way to do it.

Try something like:
1
2
3
4
5
6
7
class Socket {
public:
 Socket() { /* Do Nothing */ };
 bool connect(int family, int protocol = 0);
 bool listen();
 ~Socket() { };
};


I still don't know why Accept is sending back a new copy of the class. It's un-unnecessarily creating extra instances.
Last edited on
OK, i believe i've found a good solution. What do you think about it, it's sufficiently OOP ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Socket {

public:
    Socket(int family, int protocol = 0) 
    {
        sd = ::socket(family, protocol)
    }
    Socket accept()
    {
        return Socket(this);
    }
    ....
private:
    int sd;

    Socket(Socket * server)
    {
        socklen_t len;
        struct sockaddr remote;

        sd = ::accept(server->sd, &remote, &len);
    }
....
}
Last edited on
Ok. So to execute the accept method your code will look like:

1
2
Socket mySocket = Socket(family, protocol);
Socket myNewSocket = mySocket.accept(); // Hmmm... 


My question is obviously. Why do I need to create a socket for no reason, but to call accept() on it to create another socket?
Last edited on
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();
Last edited on
Topic archived. No new replies allowed.