Ok I have this problem which is very simple yet I can't find the solution anywhere.

I have a simple function:

bool MyClass::close(int fd)
{
 // code

 shutdown(sockFd, 2);
 close(sockFd);

 //code
}

This function does a lot of work and one of the things it needs to do it shutdown and close a socket with socket descriptor being "sockFd"

The problem is with the

close(sockFd);

call that I make to close the socket and release resources. The problem is that it has the same exact function signature as my function and so what ends up happening is a recursive call to my function rather than a call to the "close()" function to close a socket

I figured I need to somehow differentiate them so I tried using std::close() but close() is not part of that namespace.

So does anyone know where close() is defined so I could do something like "SomeClass::close()"

Oh and by the way I can't just rename my function to something else. Part of my requirements is that my function be called close().

Please if anyone has ideas I would appreciate them.

You're using the function; presumably you can work out which header it came from. close() is not a standard function in C or C++.

I'm guessing it is a function related to sockets handling under win32 or unix (typically in a header like <io.h>, but that varies). If my guess is correct, it may be unambiguously called using "::close(sockFd);"

If my guess is wrong (eg it's a macro) then your only choice would be to rename your MyClass::close() function .... for example to MyClass::finish().

::close() worked perfectly!
Would you mind explaining why?
I mean what happens when "::" is used in such a way? The reason for my curiosity is that I've never heard of this and it seems like something I should know.

::close() worked perfectly!
Would you mind explaining why?
I mean what happens when "::" is used in such a way?
The reason for my curiosity is because I've never heard of this and it seems like something I should know.
Thanks

::close() just means the function close() is within the global (unnamed) namespace.

In C++, if a function is not explicitly declared with a namespace, it is placed in the global (unnamed) namespace. That's what happens, by default, with functions from C standard headers (<stdio.h>, etc) and all third-party library functions that can be accessed from C.

If you declare a function at file scope, and don't explicitly place it in a namespace, it will also be within the unnamed global namespace. So, a function declared at file scope.

void f();

has a fully qualified name of ::f().

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.