Click to See Complete Forum and Search --> : When shoud "::mFunc()" syntax be used?
LarryChao
March 20th, 2003, 12:49 PM
Hello,
Assuming we have an object "spb", which has a member function named "getsize".
Now I want to invoke the "getsize" function. Could some one explain to me the difference between the following 2 methods:
-------method 1------------
::getsize();
-------method 2------------
spb test;
test.getsize();
-------------------------------
How are the two methods used under different circumstances ?
Thanks very much in advance,
PaulWendt
March 20th, 2003, 01:00 PM
Well, ::getsize() is a global function that seemingly operates on
nothing. test.getsize() is going to call spb's getsize() for the test
object. They're two totally different concepts.
I don't know what your global "getsize()" function does and I
don't know what your "spb::getsize()" function does.
--Paul
PaulWendt
March 20th, 2003, 01:01 PM
Oh, I understnad your question now [after reading its subject
again].
Basically, you prepend a function name with :: when you want to
call the GLOBAL version of that function.
If you're in a class that has a member function getsize(), and you
type getsize(), you're going to be calling the getsize() member
function. To call the global version, you prepend the function
with the scope-resolution operator [::].
LarryChao
March 20th, 2003, 01:07 PM
It sounds like you have to declare "getsize" as a global function outside of any object in order to use "::getsize()" syntax?
LarryChao
March 20th, 2003, 01:09 PM
Thanks very much, it's clear now.
dude_1967
March 20th, 2003, 01:36 PM
Yes indeed,
The double colon operator is called the scope resolution operator which is used to resolve namespace and certain class/structure elements. The simple double colon prefix is the scope resolution operator applied on global scale. Static functions not belonging to any class or namespace can be resolved with this.
The sample below explains all this a bit more clearly.
Chris.
:)
class c
{
public:
static void nothing(void) { }
};
void nothing(void)
{
}
namespace notta
{
void nothing(void)
{
}
}
int main(int argc, char* argv[])
{
// Global function nothing()
::nothing();
// Member function nothing()
::c::nothing();
// Namespace function nothing()
::notta::nothing();
return 1;
}
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.