0
#include<iostream>

using namespace std;

class der
{
public:
    void fun()
    {
        cout << "Good";
    }
};

int main()
{
    der a;
    a.der::fun(); // method 1
    a.fun(); // method 2

    return 0;
}

I know, that :: is used to access content of namespaces or nested class... But what is main difference between method 1 and method 2 that I mentioned in my code? Both are working fine...

Thanks.

Mateusz Grzejek
  • 11,698
  • 3
  • 32
  • 49
Asif Mushtaq
  • 3,658
  • 4
  • 44
  • 80

3 Answers3

3

a.der::fun(); just uses the explicit class scope. It doesn't make any difference in your case.

It becomes interesting, if you want to explicitly call a base classes function, that was publicly inherited by der.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • @AsifMushtaq This: `der::` – πάντα ῥεῖ May 03 '15 at 10:51
  • you mean dot can work with only object.. and :: can work with object and without object? there is no logically difference in my code? – Asif Mushtaq May 03 '15 at 10:54
  • 1
    @AsifMushtaq Yes the `.` operator only works with object instances. The `::` resolves namespaces and class scope. There's no difference for the function calls in your sample. – πάντα ῥεῖ May 03 '15 at 10:56
  • Hi, but when I tried this one.. der::fun(); // method 1 it's showing error cannot call void function without object? – Asif Mushtaq May 03 '15 at 11:06
  • @AsifMushtaq _"but when I tried this one.. `der::fun();` ..."_ That's because `fun()` is declared as a non static class member funciton. Prefix the function declaration with `static` and your mentioned sample will work. – πάντα ῥεῖ May 03 '15 at 11:08
  • That's worked can you explain it's logic,, mean static logic? Sorry I'm new to C++ – Asif Mushtaq May 03 '15 at 11:11
  • @AsifMushtaq _"Sorry I'm new to C++"_ Not that I didn't notice ;) . Well I urgently recommend you get [good book about the c++ language](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) where these basics are explained in depth. SO isn't really the suitable site to learn about these. As for your (I hope last) question: Basically `static` frees the function from a class instance context. No `this` pointer will be accessible in the functions body. – πάντα ῥεῖ May 03 '15 at 11:18
  • Static methods works just like normal global functions, but they can acces: (1) any static member variables (even private), (2) private object's variables given by reference. In example if you hve `static void A::foo(A& x);` then you can acces `x`'s private variables and private methods, which would not be able in normal, global function. – RippeR May 03 '15 at 11:19
  • Happy to help, but for next time: (1) try to write better questions, since your one was not what you wanted to know, (2) try to search for answer a little, this topic was covered like 100000 times already in any tutorial/book/etc. – RippeR May 03 '15 at 11:27
2

You can use it to call methods from a base class, sometimes it's even required to disambiguate the call. Example:

#include <iostream>

struct B
{
    void foo() { std::cout << "B::foo" << std::endl; }
};

struct B2
{
    void foo() { std::cout << "B2::foo" << std::endl; }
};

struct A : public B
{
    void foo() { std::cout << "A::foo" << std::endl; }
};

struct C : public B, public B2
{
};

int main()
{
    A a;
    a.foo(); // calls A::foo
    a.B::foo(); // calls B::foo

    C c;
    c.B2::foo(); // calls B2::foo, needed to disambiguate from B::foo

    return 0;
}

Live example

Daniel Frey
  • 55,810
  • 13
  • 122
  • 180
1

Lets take this example:

class A {
    public:
        virtual void foo() { /* ... */ }
};

class B : public A {
    public:
        void foo() { /* ... */ }
};

int main() {
    B b;
    b.foo(); // this performs B::foo() on b object
    b.A::foo(); // this performs A::foo() on b object
}

So this is so you can access methods that are overloaded by you. It doesn't even have to be virtual method, just have same name and parameteres.

RippeR
  • 1,472
  • 1
  • 12
  • 23
  • I know that.. but I was asking main difference in my code..!! I was trying to access member function without object like this one.. der::fun(); // method 1 it's showing error.. can I access member function without object? – Asif Mushtaq May 03 '15 at 11:09
  • 1
    In your example `a.der::fun(); // method 1` uses `a` object, so is `a.fun(); // method 2`. **To acces method without an object, you must declare this method as static. See [this](http://pastebin.com/SUeG2RLQ).** – RippeR May 03 '15 at 11:15
  • Though keep in mind, that static methods (like `A::bar()` in linked example) can only acces other static methods and variable of class, since they don't have references to objects. So you can't call `A::foo()` method inside `A::bar()` method unless you somehow have reference to object of type `A`. – RippeR May 03 '15 at 11:16
  • thanks, link you attached block in our country don't know why.. I know how to use static got that point from above member reply.. can you explain logic of static function related to my code? – Asif Mushtaq May 03 '15 at 11:18
  • [This](http://pastebin.com/Q7T3mtqV) is example who can use what with static member functions. **But if that concept is dificult for you, I seriously recommend you to read some tutorial or books about C++, since it's very basic concept and it might be best to just read what is already written instead of asking to write it again.** – RippeR May 03 '15 at 11:24