Sign in to follow this  
DarkNebula

Scope question

Recommended Posts

I was wondering if this is a good idea/practice...
int CSocket::connect()
{
    ::connect(sockfd, (struct sockaddr*) &remote_addr, sizeof(remote_addr));
}
The ::connect is the network socket connect if you couldn't tell. And I have a class with connect already in it, and needed to change the scope. So is this good or bad? Is there a better way? Thanks.

Share this post


Link to post
Share on other sites
The namespace resolution (?) operator, ::, is well defined.

If used without a class name or namespace prefix, it denotes the global namespace. If you have another connect() function in the global namespace with different arguments than the socket connect function, then all you've done is overload that function.

However, if you have another connect function with the same arguments, then it won't even compile.

As long as you know how that works, just code accordingly. If your class has a connect member function, then inside methods of that class, any reference to connect() will be to the member function, unless prefixed with ::, then it will be the connect() function in the global namespace.

Share this post


Link to post
Share on other sites
If you're going to be using the global function a lot and don't want to prefix your calls with "::", you can utilize the "using" keyword like so:

void Dummy(void)
{
//1
}

struct SDummy
{
void Dummy(void)
{
//2
}

void Test(void)
{
Dummy(); //2

using ::Dummy;

Dummy(); //1
this->Dummy(); //2
}
};




Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this