Overloaded Functions Problem

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2008
Posts: 5
Reputation: DeboJackson is an unknown quantity at this point 
Solved Threads: 0
DeboJackson DeboJackson is offline Offline
Newbie Poster

Overloaded Functions Problem

 
0
  #1
Jan 15th, 2009
Ok I have this problem which is very simple yet I can't find the solution anywhere.

I have a simple function:
  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
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 206
Reputation: grumpier has a spectacular aura about grumpier has a spectacular aura about 
Solved Threads: 31
grumpier grumpier is offline Offline
Posting Whiz in Training

Re: Overloaded Functions Problem

 
0
  #2
Jan 15th, 2009
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().
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 5
Reputation: DeboJackson is an unknown quantity at this point 
Solved Threads: 0
DeboJackson DeboJackson is offline Offline
Newbie Poster

Re: Overloaded Functions Problem

 
0
  #3
Jan 15th, 2009
::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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 5
Reputation: DeboJackson is an unknown quantity at this point 
Solved Threads: 0
DeboJackson DeboJackson is offline Offline
Newbie Poster

Re: Overloaded Functions Problem

 
0
  #4
Jan 15th, 2009
::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
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 206
Reputation: grumpier has a spectacular aura about grumpier has a spectacular aura about 
Solved Threads: 31
grumpier grumpier is offline Offline
Posting Whiz in Training

Re: Overloaded Functions Problem

 
0
  #5
Jan 15th, 2009
::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.
  1. void f();
has a fully qualified name of ::f().
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC