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

    c++ 11 - can i override functions without re-declare them?

    see these sample:
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class test
    {
        public:
            virtual void ola()
            {
                cout <<"hi" << endl;
            }
    
            test()
            {
                //nothing;
            }
    };
    
    class test1 : public test
    {
        public:
    
            test1()
            {
                //nothing;
            }
    };
    
    void test1::ola()
    {
        cout << "hello" << endl;
    }
    
    
    int main()
    {
    
        //cout << test::a << endl;
        return 0;
    }
    like you see, i don't re-declare the 'ola' function in 'test1' class, only in 'test' class.
    the compiler tell me the 'ola' isn't member of 'test1'. in 'test' i put it 'virtual', but forgeting that, how can i override it without re-declare it?
    Last edited by Cambalinho; September 9th, 2014 at 03:31 AM.

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

    Re: c++ 11 - can i override functions without re-declare them?

    You cannot override ola without declaring it in test1.
    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
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: c++ 11 - can i override functions without re-declare them?

    depending on what exactly you want to achieve...

    you may be able to get what you want via function pointers/function objects and lambda's. But this may be limited depending on what kind of access you expect to get to the base class.

    Code:
    class base
    {
    public:
    	base()
    	{
    		ola = [&](){ hello=1; };
    	}
    
    	void callola()
    	{
    		ola();
    	}
    
    protected:
    	int hello;
    	std::function<void()> ola;
    };
    
    
    class derived : public base
    {
    public:
    	derived()
    	{
    		ola = [&](){ hello = 2; };
    	}
    };
    
    int main()
    {
      derived d;
      d.callola();  // sets d.hello to 2
    }
    you can make ola public
    which would allow direct access to all (and change) the function on the fly
    but when called outside of of memberfunction of d, you won't have any access to d at all except through normal capture of the specific instance.

    also note that it's not the same as a virtual, the virtual is global, this is per-instance. It is possible to have multiple instaces of the same class pointing to different implementations of ola().

  4. #4
    Join Date
    Apr 2009
    Posts
    1,355

    Re: c++ 11 - can i override functions without re-declare them?

    Quote Originally Posted by OReubens View Post
    depending on what exactly you want to achieve...

    you may be able to get what you want via function pointers/function objects and lambda's. But this may be limited depending on what kind of access you expect to get to the base class.

    Code:
    class base
    {
    public:
        base()
        {
            ola = [&](){ hello=1; };
        }
    
        void callola()
        {
            ola();
        }
    
    protected:
        int hello;
        std::function<void()> ola;
    };
    
    
    class derived : public base
    {
    public:
        derived()
        {
            ola = [&](){ hello = 2; };
        }
    };
    
    int main()
    {
      derived d;
      d.callola();  // sets d.hello to 2
    }
    you can make ola public
    which would allow direct access to all (and change) the function on the fly
    but when called outside of of memberfunction of d, you won't have any access to d at all except through normal capture of the specific instance.

    also note that it's not the same as a virtual, the virtual is global, this is per-instance. It is possible to have multiple instaces of the same class pointing to different implementations of ola().
    now imagine that i want change the lambda value in Global Scope section(outside of functions\class's\others)?

  5. #5
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: c++ 11 - can i override functions without re-declare them?

    you can't.
    or rather, not as a generic solution to apply to all instances. you can only do that on a per-instance base.


    since the lambda isn't a memberfunction, it doesn't automatically have a 'this'. The 'this' needs to be part of the capture clause and since you wantr it to be defined outside of the context of the object, it won't work.



    You can create a class member pointer and change the pointer on the fly, but all the actual implementations would need to be defined as part of being class members. again, not what you seem to be aiming at.


    the closest you could get would be to take the example in #4, make the ola member static (and thus apply to all instances).
    'this' access becomes pointless, so add a (const) class reference as parameter
    change the callola() to pass the *this.
    you'll only be able to access public members since the ola() isn't a class member anymore.
    there's probably a couple cludges you could apply to circumvent that, but we're already well into abusing things as they are.

    With a question like that, I'm very much inclined to think you have a design issue, and you should go back to rethink the actual design/approach of your class.

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: c++ 11 - can i override functions without re-declare them?

    Quote Originally Posted by OReubens
    With a question like that, I'm very much inclined to think you have a design issue, and you should go back to rethink the actual design/approach of your class.
    If I remember correctly, Cambalinho actually asked this question before, or questions along similiar lines. Something about using C++ as an intermediate language, but I didn't understand why the compiler/interpreter couldn't simply output valid C++ code if that was the case.
    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

  7. #7
    Join Date
    Apr 2009
    Posts
    1,355

    Re: c++ 11 - can i override functions without re-declare them?

    Quote Originally Posted by OReubens View Post
    you can't.
    or rather, not as a generic solution to apply to all instances. you can only do that on a per-instance base.


    since the lambda isn't a memberfunction, it doesn't automatically have a 'this'. The 'this' needs to be part of the capture clause and since you wantr it to be defined outside of the context of the object, it won't work.



    You can create a class member pointer and change the pointer on the fly, but all the actual implementations would need to be defined as part of being class members. again, not what you seem to be aiming at.


    the closest you could get would be to take the example in #4, make the ola member static (and thus apply to all instances).
    'this' access becomes pointless, so add a (const) class reference as parameter
    change the callola() to pass the *this.
    you'll only be able to access public members since the ola() isn't a class member anymore.
    there's probably a couple cludges you could apply to circumvent that, but we're already well into abusing things as they are.

    With a question like that, I'm very much inclined to think you have a design issue, and you should go back to rethink the actual design/approach of your class.
    i'm trying do a sample:
    Code:
    #include <iostream>
    #include <string>
    #include <functional>
    
    using namespace std;
    
    typedef std::function<void(void)> OnSomethingHandler;
    class test
    {
    
        public:
    
            test()
            {
                //nothing;
            }
            static OnSomethingHandler ola;
    
    };
    
    //OnSomethingHandler test::ola=NULL;
    
    class test2 final : test
    {
        public:
        test2()
        {
            //nothing;
        }
    }test2;
    
    OnSomethingHandler test2::ola=[this]()
    {
        cout << "hello";
    };
    
    int main()
    {
    
        return 0;
    }
    but i get several errors:
    - "ISO C++ does not permit 'test:la' to be defined as 'test2:la' [-fpermissive]"
    - "invalid use of 'this' at top level"
    what i'm doing wrong with your idea?

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: c++ 11 - can i override functions without re-declare them?

    Quote Originally Posted by Cambalinho View Post
    but i get several errors:
    - "ISO C++ does not permit 'test:la' to be defined as 'test2:la' [-fpermissive]"
    - "invalid use of 'this' at top level"
    what i'm doing wrong with your idea?
    http://forums.codeguru.com/showthrea...s-polymorphism
    http://cboard.cprogramming.com/cplus...c-members.html
    Victor Nijegorodov

  9. #9
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: c++ 11 - can i override functions without re-declare them?

    because you are trying to declare a test2:la when no such thing has been defined.

    you can NEVER declare something without also defining it, I thought that was Obvious from your initial question. throwing a load of Sugar on it doesn't change that very fact.

  10. #10
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: c++ 11 - can i override functions without re-declare them?

    Quote Originally Posted by OReubens
    you can NEVER declare something without also defining it
    I think you meant to write "you can NEVER define something without also declaring it" since all definitions are declarations, but not all declarations are definitions (e.g., forward declarations, or declaring a virtual member function to be pure virtual without defining it).
    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

  11. #11
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: c++ 11 - can i override functions without re-declare them?

    Quote Originally Posted by Cambalinho View Post
    see these sample:
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class test
    {
        public:
            virtual void ola()
            {
                cout <<"hi" << endl;
            }
    
            test()
            {
                //nothing;
            }
    };
    
    class test1 : public test
    {
        public:
    
            test1()
            {
                //nothing;
            }
    };
    
    void test1::ola()
    {
        cout << "hello" << endl;
    }
    
    
    int main()
    {
    
        //cout << test::a << endl;
        return 0;
    }
    like you see, i don't re-declare the 'ola' function in 'test1' class, only in 'test' class.
    the compiler tell me the 'ola' isn't member of 'test1'. in 'test' i put it 'virtual', but forgeting that, how can i override it without re-declare it?
    You already asked the same question four months back here.
    Was that discussion not enough?
    Victor Nijegorodov

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