static function call
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: static function call

  1. #1
    Join Date
    Jun 2012
    Posts
    38

    static function call

    PHP Code:
    class A
    {
    public:
        static 
    void func(){std::cout<<"A\n";}
    };

    class 
    B:public A
    {
    public:
        
    virtual void func(){A::func();}
    };

    class 
    C:public B
    {
    public:    
        
    virtual void funx(){func();}
    };

    class 
    D:public C
    {
    public:
        static 
    void func(){std::cout<<"CC\n";}
        
    void Call(){funx();}
    };

    int _tmain(int argc_TCHARargv[])
    {
        
    D b;
        
    b.Call();
            return 
    0;

    I am lost in the basic *static* key applied for a function. Could someone please tell me why the above code print "A". I suppose it should be "CC".

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,043

    Re: static function call

    The compiler gives me this message
    warning C4526: 'D::func' : static member function cannot override virtual function 'void B::func(void)
    override ignored, virtual function will be hidden

  3. #3
    Join Date
    Jun 2012
    Posts
    38

    Re: static function call

    PHP Code:
    class D:public C
    {
    public:
        
    virtual void func(){std::cout<<"CC\n";}
        
    void Call(){funx();}
    }; 
    and
    PHP Code:
    class D:public C
    {
    public:
        
    void func(){std::cout<<"CC\n";}
        
    void Call(){funx();}
    }; 
    From the OP example, could you tell me which one (please pay attention to the virtual key) is polymorphically implemented ? Does the virtual key above make its actual sense ? I don't think so.

  4. #4
    Join Date
    Jun 2012
    Posts
    38

    Re: static function call

    Quote Originally Posted by GCDEF View Post
    The compiler gives me this message
    warning C4526: 'D::func' : static member function cannot override virtual function 'void B::func(void)
    override ignored, virtual function will be hidden
    I don't have this warning on my VS2012 Ultimate... Please wait a second I look into compiler option....and be back very soon

    (after 2 minutes I'm back and...)
    I have level3 of warnings and see no warning(C4526), even after I enable all warnings(/Wall)
    Last edited by terminalXXX; June 20th, 2013 at 08:08 AM.

  5. #5
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,245

    Re: static function call

    Quote Originally Posted by terminalXXX View Post
    I don't have this warning on my VS2012 Ultimate... Please wait a second I look into compiler option....and be back very soon

    (after 2 minutes I'm back and...)
    I have level3 of warnings and see no warning(C4526), even after I enable all warnings(/Wall)
    I get the same warning as GCDEF in post #2 with level 3.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  6. #6
    Join Date
    Jun 2012
    Posts
    38

    Re: static function call

    Ok thank you you two a lot, what about my third post question ?


    (Oppps, after 1 minute I posted this, I realize I was wrong because ...)

    virtual key in the base class is meant for polymophism, but in the child isn't supposed to be so unless it does have any children then, right ? So this is simply a chain of function calls, nothing magical about this between virtual and non-virtual. It is not polymorphism at all.
    Last edited by terminalXXX; June 20th, 2013 at 08:33 AM.

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,043

    Re: static function call

    Quote Originally Posted by terminalXXX View Post
    I don't have this warning on my VS2012 Ultimate... Please wait a second I look into compiler option....and be back very soon

    (after 2 minutes I'm back and...)
    I have level3 of warnings and see no warning(C4526), even after I enable all warnings(/Wall)
    Regardless, that's the problem. You can search that warning for more information.

  8. #8
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,245

    Re: static function call

    From the OP example, could you tell me which one (please pay attention to the virtual key) is polymorphically implemented ? Does the virtual key above make its actual sense ? I don't think so.
    To implement polymorphically, the function in the base class needs to be declared virtual. Derived classes from the base class can declare the overridden functions as virtual but it is not needed once the base class function has been declared virtual. Static class functions are functions that can called outside of an instance (eg from your code A::func()). These class static functions do not have access to the 'this' pointer and so cannot access class variables etc.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center