Make sure the method declaration is inherited

How can I protect against accidentally defining a non-inherited method where the inherited definition is intended. I am told that there is a trick to express it, but no one can remember it.

Explanation. I have a class tree: "Base" <- 'C' <- 'D', below. The base defines a pure virtual function. The function is redefined in C and then in D. But the function has a very long list of arguments.

Somewhere along the derivation chain in agrglist there is a subtle mistake that makes D :: non-inherited. The program is flexibly compiled. And the wrong method is called at runtime.
Is there a trick to cause a compilation error when the method is not inherited.

#include <iostream>

class Base {
public:
    virtual void VeryLongFunctionName(int VeryLongArgumentList) = 0;
};
class C : public Base {
public:
    void VeryLongFunctionName(int VeryLongArgumentList) {
        std::cout << "C::\n";
    }
};
class D : public C {
public:
    void VeryLongFunctionNane(int VeryLongArgumentList) { // typo is intentional. It the point of the question.
        std::cout << "D::\n";
    }
};

int main() {
    Base *p = new D;
    p->VeryLongFunctionName(0);
            // the intention is to print D::. But it prints C::.
            // How can we make compiler catch it.       
    return 0;
}
+5
3

++ 0x override, V++ 2005 : http://msdn.microsoft.com/en-us/library/41w3sh1c.aspx

, V++ (, ):

#include <iostream>

class Base {
public:
    virtual void VeryLongFunctionName(int VeryLongArgumentList) = 0;
};

class C : public Base {
public:
    void Base::VeryLongFunctionName(int VeryLongArgumentList) {
        std::cout << "C::\n";
    }
};

class D : public C {
public:
    void Base::VeryLongFunctionNane(int VeryLongArgumentList) {
    //   ^^^^^^ now causes a compilation error
        std::cout << "D::\n";
    }
};
+2

, , , :

class t_very_long_argument_list {
public:
    t_very_long_argument_list(T1& argument1, const T2& argument2);
    /* ... */
    T1& argument1;
    const T2& argument2;
};

int C::VeryLongFunctionName(t_very_long_argument_list& arguments) {
    std::cout << "C::\n";
}
+4

-

  • int VeryLongFunctionName(int VeryLongArgumentList) int, .
  • int VeryLongFunctionName(int VeryLongArgumentList) int.

    p > VeryLongFunctionName();//

. : http://ideone.com/wIpr9

+1

All Articles