dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2009
    Posts
    1,228

    [RESOLVED] why i must use an object when i have a virtual function on a class?

    i have 2 class's:
    Code:
    class test{
    
    
    public:
        virtual void MouseClick(){;};
        virtual void Move(){;};
    	test()
    	{
            MouseClick();
            Move();
    	}
    
    
    	void call()
    	{
    
    
            MouseClick();
            Move();
    	}
    };
    void Empty()
    {
        //nothing;
    }
    class InstanceName : public test
    {
        public:
        virtual void MouseClick();
    };//i wanted these way: "}InstanceName;" but error too
    
    void InstanceName::MouseClick()
    {
        std::cout << "Mouse click\n";
    }
    
    int main()
    {
        InstanceName::call();//error
        InstanceName::call();
        std::cin.get(); //press enter before exit
        return 0;
    }
    why, when i use virtual functions, i can't use the class name for call the methods?
    error message: "cannot call member function 'void test::call()' without object"

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,748

    Re: why i must use an object when i have a virtual function on a class?

    It is a non-static member function, so it must be called via an object. That it is a virtual function is secondary.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Feb 2017
    Posts
    296

    Re: why i must use an object when i have a virtual function on a class?

    Quote Originally Posted by Cambalinho View Post
    why, when i use virtual functions, i can't use the class name for call the methods?
    Virtual or non-virtual, to call a non-static method of a class you need an object of the class. To call a method without an object the method must be declared static.

    So a static member belongs to the class whereas a non-static member belongs to an object of the class.
    Last edited by wolle; June 3rd, 2018 at 06:25 AM.

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,273

    Re: why i must use an object when i have a virtual function on a class?

    'void test::call()'

    If you want to call a member function like this, then the function must be static - and it then doesn't have ready access to non-static class data.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.7.4)

  5. #5
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,097

    Re: why i must use an object when i have a virtual function on a class?

    Additionally, a static method cannot be virtual. So, if you want to call a method without having an instance of the object, you need a static method, and you need to specify the exact name of the class of which you want to call the static method, that is, polymorphic behavior is not possible with static methods. If you want to use polymorphic behavior, then you need to use non-static methods, and then you need to have instance of the object on which you call the method.

  6. #6
    Join Date
    Apr 2009
    Posts
    1,228

    Re: why i must use an object when i have a virtual function on a class?

    thank you so much to all for correct me. thank you

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)


×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.