This forum has been archived. Please start a new discussion on GitHub.

Question about souce codes "TcpEndpoint::acceptor"

In the TcpEndpoint.cpp file,

IceInternal::TcpEndpoint::acceptor(EndpointPtr& endp) const
TcpAcceptor* p = new TcpAcceptor(_instance, _host, _port);

endp = new TcpEndpoint(_instance, _host, p->effectivePort(), _timeout, _compress);

return p;

I can't understand the lines with "new" . why need such codes? :confused: Consideration for reference number of smart pointer?

sorry for this trivial question.
Thanks in advance.


  • benoit
    benoit Rennes, France

    The "endp" argument is passed by reference and is used as a return value for the accept() method. The use of a smart pointer ensures that the caller won't have to take care of the deallocation of the TcpEndpoint object.

    I hope this is clearer!

  • hi, Benoit,
    Sorry for my poor expression about my question. I mean, why need to allocate a new TcpEndpoint here? I traced into the code and found that the members of the new TcpEndpoint seem to have the same values as the one passed in by "EndpointPtr& endp" except the address and the ref. Why not use the original TcpEndoint in the accept() method ?
    There must be some thing out of my understanding. Hope getting your help.

    Best Regards---OrNot
  • benoit
    benoit Rennes, France
    I don't think it's exactly the same -- the port might be different for instance.

  • Thank you Benoit.
    Maybe the method "effectivePort()" is the key. I will test it further.

  • marc
    marc Florida
    If you do not specify a port number, the operating system will choose one. endp returns an endpoint with that port number.
  • Oh yeah! :)