Template method by method without templates in a derived class

class A {
public:
    template<typename T> void func(size_t n, T values[]) { ... }
};

class B : public A {
public:
    void func(size_t n, uint32_t values[]) { ... }
};

Why, when calling this code, B::func() does a function take precedence over a function template A::func()?

uint32_t values[5];
A* obj = new B();
obj->func(5, values);
+3
source share
4 answers

Two reasons -

  • In C ++, a member function only overrides the member function of the base class if the base class function is marked as virtual. Otherwise, C ++ treats these two as independent functions, which coincidentally have the same name. This contrasts with Java, where functions atomically override base class functions.

  • ++ . , - vtables, ++. ++ . vtable, vtable , , . , ++ .

+6

func // A, A::func, func ( : ).

?

+1

A::func() virtual, , B::func() . .

0

B:: func A:: func , , . A:: func , . , , . ++.

B *, A *, , .

0
source

Source: https://habr.com/ru/post/1783018/


All Articles