• Advertisement
Sign in to follow this  

Scope question

This topic is 4203 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
should function correctly given that it is not derived from mfc winsock

Kuphryn

Share this post


Link to post
Share on other sites
Thanks for clearing that up. And I like the using ::connect idea! I'll keep that in mind. Thanks very much. :-D

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement