CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2002
    Location
    ireland
    Posts
    291

    calling non-static methods statically

    Why is it possible to call a non-static method with static syntax , is it only for inherited classes ?



    Base::func() //virtual function
    {

    ..

    }

    Derived:func() //Base is the parent
    {
    if(//something)
    //do something

    else
    Base::func(); //call the base class version

    }

  2. #2
    Join Date
    Dec 2004
    Location
    Poland
    Posts
    1,165

    Re: calling non-static methods statically

    Calling is NOT static. Derrived class has 2 versions of func(): its own, and from base class, but hidden, overriden. When you write Base::func() in derrived class, its same as this->(Base::func()) call, which calls overriden method, and invisible without using scope operator.

    Hope made it clear

    Hob
    B+!
    'There is no cat' - A. Einstein

    Use [code] [/code] tags!

    Did YOU share your photo with us at CG Members photo gallery ?

  3. #3
    Join Date
    Aug 2002
    Location
    ireland
    Posts
    291

    Re: calling non-static methods statically

    Thanks.
    So it's only allowed within inherited classes or could it be used with non-related classes (maybe if forward declaration were used) ?

    class ClassC; //forward declaration


    ClassA::func()
    {
    ClassC::somefunc(); //not related

    }

  4. #4
    Join Date
    Dec 2004
    Location
    Poland
    Posts
    1,165

    Re: calling non-static methods statically

    in this way ClassC::somefunc() should be declared as static. In 'unrelated' way it is possible to do like:
    Code:
    class Base
    {
     public:
     int func();
    }
    
    class Derived: public Base
    {
     public:
     int func();
    }
    
    class Unrelated
    {
     int somefunc()
     {
      Derived obj;
      return obj.(Base::func());
     }
    }
    As you see, it is NOT static, because function has to be called for some object ('obj' in this example). In your first example the object for which the function was called is pointed by 'this', and its possible because derrived class contains object of its base class. In your second example, ClassC::somefunc() is not called for any object, even for 'this', since 'this' cannot point to object of unrelated class ClassC.

    Quite complicated
    B+!
    'There is no cat' - A. Einstein

    Use [code] [/code] tags!

    Did YOU share your photo with us at CG Members photo gallery ?

  5. #5
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: calling non-static methods statically

    I think you are actually confused by the scope operator :: .

    Code:
    class base
    {
    public:
      void foo() { cout << "base foo" << end; } // not-virtual
    
      virtual void goo() {cout << "base goo " << endl; } // virtual
    
      static void moo() { cout << "base moo" << endl; }
    };
    
    class derived : public base
    {
    public:
      void goo() 
      {
            base::goo(); 
            // :: is used to indicate that the goo function called is the one 
            // from the base class; otherwise you get an infinite recursion
            // becase goo from derived will be called
            cout << "derived goo " << endl; 
      }
    
      static void moo2() { cout << "derived moo2" << endl; }
    
      void hoo()
      {
         base::moo(); // calls moo from base, which is static
      } 
    };
    
    int main()
    {
      base::moo();  // OK, call static moo from base
      derived::moo2(); // ok, call static moo2 from derived
    
      base::foo();  // WRONG, foo is not static
    
      base b;
      b.foo();  // ok
      b.goo(); // ok prints "base goo"
      
      derived d;
      d.goo(); // ok, calls goo from derived, prints "base goo" followed by "derived goo"
      d.base::goo(); // ok, calls goo from base, prints "base goo"
    
      d.moo2(); // ok
      d::moo2(); // wrong, d is not a namespace or class
    
      return 0;
    }
    Hope this helps you.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured