Scope question

Started by
3 comments, last by DarkNebula 17 years, 9 months ago
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.
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.
daerid@gmail.com
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	}};
should function correctly given that it is not derived from mfc winsock

Kuphryn
Thanks for clearing that up. And I like the using ::connect idea! I'll keep that in mind. Thanks very much. :-D

This topic is closed to new replies.

Advertisement