What are member overloaded and member overloaded pointers?

I am reading C ++ programming language of Bjarne Stroustrup. I came across an expression in the templates section.

The template argument can be a constant expression (§C .5), the address of an externally linked object or function (§9.2), or a non-overloaded element pointer (§15.5).

What is an overloaded member pointer? Can anyone provide an example?

+3


source to share


1 answer


Non-overloaded pointer-to-member is a name state - a pointer to an element that the superclass did not overload. Here's an example I just put together that will and won't work:

#include <iostream>

class Foo {
public:
    virtual void printN() { std::cout << 42; }
};

class Bar : public Foo {
public:
    void printN() { std::cout << 31; }
};

template <typename T, typename C>
class SomeTemplate {
public:
    void PrintData(T d) { C c; (c.*d)(); }
};

int main() {
    SomeTemplate<void (Foo::*) (), Foo> t; // Compiles - '42'
    t.PrintData(&Foo::printN);

    SomeTemplate<void (Bar::*) (), Bar> t; // Compiles - '31'
    t.PrintData(&Bar::printN);

    SomeTemplate<void (Foo::*) (), Bar> t; // Won't compile, can't convert overloaded pointers
    t.PrintData(&Foo::printN);
    return 0;
}

      



In PrintData, an instance of the class is instantiated, and the item pointer passed to it is dereferenced in the instance version of the class, causing it to call the calling function.

Templates make this approach more flexible, but I have not yet found a reason to use such code in a real-world situation, however - if anyone can find one that I would like to enlighten.

0


source







All Articles