Overied virtual method not called

Hi

I have a class with 2 functions with the same name, one virtual and one non virtual as
1
2
3
4
5
6
7

class Foo {
public:
virtual const Token*   next() const;    // function 'nextv'
                   Token*& next();             // function 'nextnv'
}


The caller is using the next() function as
1
2
3
4
5
6
7
8

Foo* foo;
const Token* token;
if (foo->next() == nullptr)                // statement 1
    foo->next() = new Token();        // statement 2
else
    token = foo->next();                  // statement 3


My issue:
I am expecting that
in statement 1 the function 'nextv' be called
in statement 2 the function 'nextnv' be called
in statement 3 the function 'nextv' be called

but with VS2017, the function 'nextnv' is always called. What is wrong with my guess ?

Last edited on
You can use the keyword override to make sure that a function is actually overridden:

http://en.cppreference.com/w/cpp/language/override
¿what about using sane function names?
Topic archived. No new replies allowed.