dcsimg
Results 1 to 8 of 8
  1. #1

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,158

    C++: can i do polymorphism without virtual functions?

    - (for me, on my case) i can't use the 'virtual' functions, because if i add the function prototype, i must define it;
    - i can use a normal functions, but only class base function is called... can resolve the errors, from what i need, but only calls the base functions.
    so is there another way for use polymorphism?
    VB6 2D Sprite control

    To live is difficult, but we do it.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    98,682

    Re: C++: can i do polymorphism without virtual functions?

    It sounds like what you want is a pure virtual function, i.e. one for which you don't provide an implementation in the base class and then are required to override in a derived class.

    https://msdn.microsoft.com/en-us/library/c8whxhf1.aspx
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,158

    Re: C++: can i do polymorphism without virtual functions?

    i'm sorry, i had tried that without success
    my problem here is: if i add a function prototype, on derived class, the compiler is give me an error, because the base class call it.
    for resolve these problem i must test if the function is defined or not.
    for these i tried use macros, but for it work, i must define something for test it.
    but do you have more ideas?
    theres too the templates (SFINAE), but i must learn them
    VB6 2D Sprite control

    To live is difficult, but we do it.

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    98,682

    Re: C++: can i do polymorphism without virtual functions?

    I'm afraid I can't help you further. I haven't done anything with C++ for about 15 years so my knowledge is hazy and outdated.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,158

    Re: C++: can i do polymorphism without virtual functions?

    using lambdas?
    Code:
    class test
    {
        public:
        template<typename ClassDerived>//getting the derived pointer
        test(auto *clsPointer)
        {
            std::function<void(void)> func =clsPointer->MouseClick;
            if(func)
                clsPointer->MouseClick();
            else
                MouseClick();
        }
        void MouseClick(){};
        void Move(){};
    };
    
    class InstanceName : public test
    { // manual expansion of ClassWithEvents
    public:
    	InstanceName() : test(this)//heres the problem here 'this' for a lambda
             {;}
    
    	~InstanceName(){;}
    
    public:
    	void MouseClick();
    	void Move();                     
    } InstanceName;
    VB6 2D Sprite control

    To live is difficult, but we do it.

  6. #6
    Hyperactive Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    497

    Re: C++: can i do polymorphism without virtual functions?

    All advice is offered in good faith only. 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/

    C++17 Compiler: Microsoft VS2017 (15.7.2)

  7. #7
    Fanatic Member PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Newport, UK
    Posts
    773

    Re: C++: can i do polymorphism without virtual functions?

    Quote Originally Posted by joaquim View Post
    using lambdas?
    Code:
    class test
    {
        public:
        template<typename ClassDerived>//getting the derived pointer
        test(auto *clsPointer)
        {
            std::function<void(void)> func =clsPointer->MouseClick;
            if(func)
                clsPointer->MouseClick();
            else
                MouseClick();
        }
        void MouseClick(){};
        void Move(){};
    };
    
    class InstanceName : public test
    { // manual expansion of ClassWithEvents
    public:
    	InstanceName() : test(this)//heres the problem here 'this' for a lambda
             {;}
    
    	~InstanceName(){;}
    
    public:
    	void MouseClick();
    	void Move();                     
    } InstanceName;
    It might be easier if you explained what you are trying to achieve and exactly why virtual methods, and related ideas like pure virtual methods, aren't suitable for what you are trying to do.

    After reading this thread and the two linked threads I am still not sure what you are trying to achieve as an end result here.

  8. #8
    Hyperactive Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    497

    Re: C++: can i do polymorphism without virtual functions?

    Over on Codeguru, what we think he wants is something like

    Code:
    template <typename CBase>
    class test
    {
    	CBase* atPointer = nullptr;
    
    public:
    	test(CBase *clsPointer) : atPointer(clsPointer) {}
    
    	void call()
    	{
    		if (MEMBER_EXISTS(atPointer, MouseClick))
    			atPointer->MouseClick();
    		else
    			std::cout << "MouseClick default" << std::endl;
    	}
    };
    
    class ClassBase {
    public:
    };
    
    class ClassDerivedM : public ClassBase {
    public:
    	void MouseClick() {
    		std::cout << "MouseClick REAL implementation" << std::endl;
    	}
    };
    
    class ClassDerivedNoM : public ClassBase {
    public:
    };
    We've offered

    Code:
    #include <iostream>
    
    template <typename CBase>
    class test
    {
    	CBase* atPointer = nullptr;
    
    public:
    	test(CBase *clsPointer) : atPointer(clsPointer) {}
    
    	void call()
    	{
    		atPointer->MouseClick();
    	}
    };
    
    class ClassBase {
    public:
    	virtual void MouseClick() {
    		std::cout << "MouseClick NULL implementation" << std::endl;
    	};
    };
    
    class ClassDerivedM : public ClassBase {
    public:
    	void MouseClick() override {
    		std::cout << "MouseClick REAL implementation" << std::endl;
    	}
    };
    
    class ClassDerivedNoM : public ClassBase {
    public:
    };
    
    int main()
    {
    	ClassBase c1;
    	ClassDerivedM c2;
    	ClassDerivedNoM c3;
    
    	test<ClassBase> t1(&c1);
    	test<ClassBase> t2(&c2);
    	test<ClassBase> t3(&c3);
    
    	t1.call();
    	t2.call();
    	t3.call();
    }
    which displays

    Code:
    MouseClick NULL implementation
    MouseClick REAL implementation
    MouseClick NULL implementation
    ???
    All advice is offered in good faith only. 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/

    C++17 Compiler: Microsoft VS2017 (15.7.2)

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width


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