I am in the process of updating and modifying source code for an existing application that runs on Linux systems. As part of my modifications, I will be needing to use the library function unlink() from unistd.h However, the class that I need to modify, has an existing public member function called "unlink" that I do not want to use. Is there any way that I can tell the compiler to use the library function rather than the member function?

Can you get rid of the unlink function in you class?

No. It is a public member function and other code depends on this class and calls that function.

is the unlink function in the library part of a namespace?

It doesn't appear to be. This is a link to the library source, maybe I am missing something.

I'm not sure if this is legal to do but maybe

namspace unistd
{
    #include "unistd.h"
}

// later on
unistd::unlock();

If that doesn't work I'm not sure if it is possible but you could always change the name of your class function.

I just tried your suggestions, but unfortunately it doesn't appear to be working.
When I use it, I am getting a compiler error of "undefined reference to ClassName::unistd::unlink(const char*)"

Looking more closely at the code, I am seeing that the class member function does not have the same signature as the library unistd.h version.

//class member function
 void unlink (void)

       
    //unistd.h function
   int unlink (const char*)

But, the compiler still tries to use the member function in all cases, even when the arguments are right for the unistd.h version.
The compiler error message is something like "undefined reference to ClassName::unlink(const char*)" and suggests using ClassName::unlink(void) instead

I thought that the compiler would automatically use the correct function as long as the signatures are different, but it doesn't appear to be doing that in this case.

If it allows you to put it in a namespace theb you might ne able to make unlink a member function and then call the namespace function inside the member function.

int ClassName::unlink(const char* foo)
{
     return uinstd::unlink(foo);
}

Thanks for the help. I have come up with a similar solution, but it isn't very elegant, so I would be happy to know of a different way.
I simply defined a non-member function unlink_ and had that function call unlink() like this:

int unlink_ (const char* path)
{
return unlink(path);
}

It seems to work, which solves the problem. I was just hoping to find a better way, but it seems that may not exist.
Anyway, thank you for your help.

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.