Thread: why redeclare virtual functions for overloading?

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    451

    why redeclare virtual functions for overloading?

    i understand that virtual functions are functions for be overloading, inside of a class.
    but if the are for overloading. why we need redeclare them?

    Code:
    class test
    {
    public:
        virtual void printed();
        void write(string message)
        {
            cout << message;
            printed();
        }
    };
    
    class b: public test
    {
          void printed();
    
    }b;
    
    void b::printed()
    {
        count << "\nhi";
    }
    
    class c: public test
    {
        void printed();
    }c;
    
    void c::printed()
    {
        count << "Hello";
    }
    like you see, the printed is a virtual function(can be overload). but i must redeclare it inside of class c and b. but if they are for be overload, why we need redeclare them?

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    Those aren't overloads; those are overrides.

    You need them to tell the compiler about them.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  3. #3
    Registered User
    Join Date
    Apr 2014
    Posts
    1
    You don't need to redeclare them in most cases. See the following:
    Code:
    #include <iostream>
    
    struct base
    {
        virtual void foo()
        { std::cout << "base" << std::endl; }
    };
    
    struct derived1 final : base 
    { }; // derived1 is-a base, so foo() is available
    
    struct derived2 final : base 
    {
        /* can be used to assign a new protection level of base members
            (from protected -> public, for example) or explicitly forward */
        using base::foo;
    };
    
    struct derived3 final : base 
    {
        /* override added in C++11 but not required for this to work */
        void foo() override 
        { std::cout << "derived3" << std::endl; }
    };
    
    int main()
    {
        derived1 d1;
        d1.foo();
        
        derived2 d2;
        d2.foo();
        
        derived3 d3;
        d3.foo();
    }
    Last edited by jeaye; 04-30-2014 at 03:21 PM.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Just because the compiler could in principle deduce that you are overriding a virtual function, doesn't mean such function definitions should act inherently different than other function definitions.

    Imagine two possible rules:

    1. All functions which are part of a class must be declared.

    2. All functions which are part of a class must be declared, unless that function is an override of a virtual function in a base class.

    Which of these rules is easier to remember? Which of these rules is simpler? Which of these rules provides more consistency in the language?

    For one thing, consumers of code don't necessarily get to see the source. They only need the declarations. It is valuable, as a user of class B, to know that B overrides a virtual function. Why would we hide this information from the user of the class?

    If we go this route, where should we stop? Maybe we don't need to declare variables, because their types can often be inferred from their usage...
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    You don't need to redeclare them in most cases.
    O_o

    You do in the situations invoked.

    override added in C++11 but not required for this to work
    That is because the `override' specifier doesn't enable the option to override a virtual function; the `override' specifier instead guarantees that a matching virtual function exists to override.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I should also note that you can use the override specifier in VS2010, although it generates a warning by default, which is easily disabled.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    Quote Originally Posted by iMalc View Post
    I should also note that you can use the override specifier in VS2010, although it generates a warning by default, which is easily disabled.
    but i continue with re-declaration, and i need avoid that
    my big objective is avoid the re-declaration
    how can i declare functions, in base, for be changed in another class derived from base without re-declare them?
    Last edited by joaquim; 05-03-2014 at 04:26 AM.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by joaquim View Post
    but i continue with re-declaration, and i need avoid that
    my big objective is avoid the re-declaration
    how can i declare functions, in base, for be changed in another class derived from base without re-declare them?
    You can't.

    A base class determines if a derived class is permitted to override a member function. It does that by declaring the function virtual.

    It is the derived class that determines if it does an override. The purpose of the declaration in the derived class is to alert the compiler that it is doing so.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by joaquim
    my big objective is avoid the re-declaration
    how can i declare functions, in base, for be changed in another class derived from base without re-declare them?
    Does it really matter? You are going to define the override anyway, and that is more work than a forward declaration. Perhaps you are concerned about names from the base class becoming hidden (e.g., because you added an overload instead of or in addition to an override), but that can be fixed by a using declaration.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    "Does it really matter?"
    depends for what you need... in my case, yes.
    sorry if don't make sence to some of you.. realy.. sorry. but i need these.
    the question can be stupid, but make sence: why re-declare virtual functions, if they can be overrrided?

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by joaquim View Post
    "Does it really matter?"
    depends for what you need... in my case, yes.
    sorry if don't make sence to some of you.. realy.. sorry. but i need these.
    the question can be stupid, but make sence: why re-declare virtual functions, if they can be overrrided?
    Because the purpose of the redeclaration, as I said in previous post, is to specify that the inherited function is being overridden. The declaration in the base class is only specifying that it can be, not that it will be.


    There are times when you have to accept "you can't" as an answer. That is the case here.
    Last edited by grumpy; 05-03-2014 at 05:07 AM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by joaquim
    depends for what you need... in my case, yes.
    What is your case?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    Quote Originally Posted by laserlight View Post
    What is your case?
    convert a new language to C\C++ language. that's why i need avoid the re-declaration of functions that need be overrrided(if i want).

  14. #14
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by joaquim View Post
    convert a new language to C\C++ language. that's why i need avoid the re-declaration of functions that need be overrrided(if i want).
    Then the converter (e.g. a compiler for your language) needs to implement machinery so it outputs correct C/C++ code even in such a case.

    I think you'll find that, unless the rules of your "new language" are rather proscriptive, that it will not be possible. The fact you haven't thought thought all the potential cases in your language does not mean that C++ does things wrong.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  15. #15
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    Quote Originally Posted by grumpy View Post
    Then the converter (e.g. a compiler for your language) needs to implement machinery so it outputs correct C/C++ code even in such a case.

    I think you'll find that, unless the rules of your "new language" are rather proscriptive, that it will not be possible. The fact you haven't thought thought all the potential cases in your language does not mean that C++ does things wrong.
    i never said the C++ does things wrong
    think in these way: why lose 3 days for doing a program, if you can do it in less than that!?!(for example)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. virtual functions, when to use them. Or not.
    By Subsonics in forum C++ Programming
    Replies: 7
    Last Post: 09-29-2010, 03:10 PM
  2. Virtual functions
    By SterlingM in forum C++ Programming
    Replies: 12
    Last Post: 11-03-2009, 11:51 AM
  3. virtual functions
    By lord in forum C++ Programming
    Replies: 9
    Last Post: 10-18-2008, 02:55 PM
  4. Virtual Functions
    By warfang in forum C++ Programming
    Replies: 2
    Last Post: 05-07-2007, 11:14 PM
  5. Virtual functions but non-virtual destructor
    By cunnus88 in forum C++ Programming
    Replies: 4
    Last Post: 03-31-2007, 11:08 AM