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.
Scope question
I was wondering if this is a good idea/practice...
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.
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.
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 }};
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement