|
-
March 20th, 2003, 01:49 PM
#1
When shoud "::mFunc()" syntax be used?
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,
Larry
-
March 20th, 2003, 02:00 PM
#2
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
-
March 20th, 2003, 02:01 PM
#3
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 [::].
-
March 20th, 2003, 02:07 PM
#4
It sounds like you have to declare "getsize" as a global function outside of any object in order to use "::getsize()" syntax?
Larry
-
March 20th, 2003, 02:09 PM
#5
Thanks very much, it's clear now.
Larry
-
March 20th, 2003, 02:36 PM
#6
Scope resolution operator
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.
Code:
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;
}
You're gonna go blind staring into that box all day.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|