943,699 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 386
  • C++ RSS
Jan 15th, 2009
0

Overloaded Functions Problem

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

I have a simple function:
C++ Syntax (Toggle Plain Text)
  1. bool MyClass::close(int fd)
  2. {
  3. // code
  4.  
  5. shutdown(sockFd, 2);
  6. close(sockFd);
  7.  
  8. //code
  9. }

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
Quote ...
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
DeboJackson is offline Offline
5 posts
since Aug 2008
Jan 15th, 2009
0

Re: Overloaded Functions Problem

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().
Reputation Points: 193
Solved Threads: 32
Posting Whiz in Training
grumpier is offline Offline
206 posts
since Aug 2008
Jan 15th, 2009
0

Re: Overloaded Functions Problem

::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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
DeboJackson is offline Offline
5 posts
since Aug 2008
Jan 15th, 2009
0

Re: Overloaded Functions Problem

::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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
DeboJackson is offline Offline
5 posts
since Aug 2008
Jan 15th, 2009
0

Re: Overloaded Functions Problem

::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.
C++ Syntax (Toggle Plain Text)
  1. void f();
has a fully qualified name of ::f().
Reputation Points: 193
Solved Threads: 32
Posting Whiz in Training
grumpier is offline Offline
206 posts
since Aug 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Help Me Please !!!
Next Thread in C++ Forum Timeline: Outputting formatted data to file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC