Thread: use of ::

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    14

    use of ::

    Howdy yall,
    I only recall a half semester of c++ before going on to Java for two years so if yall could help me with this simple question, it would be greatly appreciated...

    when do you know to use...

    void Class::methodName(){
    ...
    }

    instead of...

    void methodName() {
    ...
    }

    when defining a method?

    thanks -Cowboy

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Err... The first one when the method is part of a class, and the second when it is not part of a class. For more on classes, you can go to the tutorials on this site, or search this forum (I know Stack Overflow made a nice post on classes recently -- I'll see if I can find it).

    *edit*
    The Brain has a nice post on them here: http://cboard.cprogramming.com/showthread.php?t=59027

    Elad makes some good points on them here:
    http://cboard.cprogramming.com/showthread.php?t=58661

    Hmm... I can't find Stack Overflow's so I may be lying about that one (although I could've sworn there was one recently). :shrugs:
    Those others should help, though.
    Last edited by Zach L.; 11-26-2004 at 01:52 PM.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    14
    I understand the class idiology, Java is OO. But let me link you to where i got my confusion...

    http://www.cprogramming.com/tutorial/lesson18.html

    the default constructor uses :: but the destroy_tree does not. They are both declared in the same class (btree) tho...

  4. #4
    Registered User
    Join Date
    Nov 2004
    Posts
    1

    use of ::

    :: operator is called scope resolution operator.

    so a declaration liek this classA::somefunction()
    would mean that you are telling compiler that the function somefunction belongs to class calssA.

    So whenevr u use the dot( . ) operator with an object to classA you will ahve access to somefunction for that class (ofcourse if it is within the scope of caller) and not somefunction of class B.

    :: is also used to call methods of classes direclty using the clas sname without declaring the objject first.

    so i can call someFunction in two ways

    classA objA;
    objA.someFunction();

    or

    classA::someFunction();

    declaring a function without :: operator and class name gives it global scope so you can jsut call it like this

    someFunction();
    as long as you include the header file.

    Hope It helps

    venAdder

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Appears to be a typo in the tutorial.
    It should be 'void btree::destroy_tree(node* leaf)'.

    The only time you would not is when you define the function inside the declaration of the class. E.g.
    Code:
    class foo
    {
       void bar1( )
       {
          // Stuff here...
       }
    
       void bar2( );
    };
    
    void foo::bar2( )
    {
       // Stuff...
    }
    Last edited by Zach L.; 11-26-2004 at 02:05 PM.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  6. #6
    Registered User
    Join Date
    Nov 2004
    Posts
    14
    so a method declared with :: can be a static method or it can be inheirited when an object is declared?

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Static methods belong to the class and not to any instance of the class. That is to say, when you call them, there is no 'this' pointer sent to them. Consequently, they cannot modify non-static data of the class. They can be called without an object, though, as in 'SomeClass::SomeFunc( )'. Normal, non-static functions cannot be called this way.

    *edit*
    You can also use the scope resolution operator (::) when calling a base-class method in a derived class, which somewhat breaks the rule above, but is somewhat of a special case.
    Last edited by Zach L.; 11-26-2004 at 02:10 PM.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  8. #8
    Hello,

    Member functions maybe defined outside the structure, by using the name of the structure, followed by a scope resolution operator, followed by the name of the member function. The scope resolution operator in C++ is, ::

    The scope resolution operator is also used to qualify hidden names so that you can still use them. You can use the unary scope operator if a file scope name is hidden by an explicit declaration of the same name in a block or class. For example:
    Code:
    int i = 10;
    
    int f(int i) {
    	return i ? i : :: i;	// return global i if local i is zero
    }
    You can also use the class scope operator to qualify class names or class member names. If a class member name is hidden, you can use it by qualifying it with its class name and the class scope operator. Whenever a name is followed by a :: operator, the name is interpreted as a class name.

    In the following example, the declaration of the variable X hides the class type X, but you can still use the static class member count by qualifying it with the class type X and the scope resolution operator.
    Code:
    #include <iostream>
    using namespace std;
    
    class X {
    public:
    	static int count;
    };
    
    int X::count = 10;			// define static data member
    
    int main() {
    	int X = 0;			// hides class type X
    	cout << X::count << endl;	// use static member of class X
    
    	return 0;
    }
    The Binary and Unary scope resolution operator's associativities are read left-to-right. Keep in mind the scope resolution and global scope operators are different. For example:
    Code:
    Operator	Description		Usage and Notes
    ::		scope resolution	class_name :: member
    ::		global			:: name
    All in all, you can use the scope resolution operator to reference global variables, to reference hidden members in base classes, to explicitly reference a member that is inherited, or to otherwise name a member hidden by the current context. When you are in the context of a nested class, you must use the scope resolution operator to access members of the enclosing class.


    - Stack Overflow
    Last edited by Stack Overflow; 11-26-2004 at 02:21 PM. Reason: Fixed Typo
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  9. #9
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Okay... Now there is a nice post on the topic by Stack Overflow.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  10. #10
    Registered User
    Join Date
    Nov 2004
    Posts
    14
    i ran out of ram reading that post...let me try it again lol

  11. #11
    Quote Originally Posted by Zach L.
    Okay... Now there is a nice post on the topic by Stack Overflow.
    Heh. I never recalled making a post about this before, so I thought I'd try!

    Quote Originally Posted by Zach L.
    Hmm... I can't find Stack Overflow's so I may be lying about that one (although I could've sworn there was one recently).
    Ah, don't worry about it. It was right under your nose.

    Quote Originally Posted by Cowboy
    i ran out of ram reading that post...let me try it again lol
    Ah. A 256MB stick can do wonders, sometimes.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  12. #12
    Registered User
    Join Date
    Nov 2004
    Posts
    14
    oh my dimm slots are full, its too bad they dont make ram for my brain...

    ok so...methods declared outside main have the scope resolution...methods inside main dont have the resolution...and scope resolution is also used when main and class methods or variables have the same name...

  13. #13
    Alright,

    My examples may have been convincing, though the truth underlying the scope resolution operator is this:
    • Reference global variables
    • Reference hidden members in base classes
    • Explicitly reference a member that is inherited [or to otherwise name a member hidden by the current context]
    • Access members of the enclosing class when in the context of a nested class

    It is quite simple. My examples showed the use of these somewhat and are not the only ways the scope resolution operator can be used. If you keep the 4 key points of the operator, everything else should become clear.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  14. #14
    Registered User
    Join Date
    Nov 2004
    Posts
    14
    man you read like my text book lol...

    Its pieceing together now, thank you...have you ever considered writing a c++ book (even though there are billions out there)? lol

  15. #15
    Quote Originally Posted by Cowboy
    man you read like my text book lol...

    Its pieceing together now, thank you...have you ever considered writing a c++ book (even though there are billions out there)? lol
    Glad to be of assistance. I apologize if I came across with too much detail and very little example. Though I have not considered writing a C or C++ book. I may sometime in the future, but I'm still in the learning process. I teach what I know to my best ability.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

Popular pages Recent additions subscribe to a feed