I want to have a class that I can inherit from and overwrite the function that creates the value for the [] operator. Why do these not have the same result? Is there a way to get the second result without using a pointer?
Code:
#include <iostream>
using std::cout;
class session {
public:
session(){}
~session(){}
void operator[](const char* c) {foo(c);}
protected:
virtual void foo(const char* c) {cout << "parent called" << "\n";}
};
class session2 : public session {
public:
session2(){}
~session2(){}
protected:
virtual void foo(const char* c) {cout << c << "\n";}
};
int main() {
session2 s2;
session a = s2;
a["child called"];
session* b = &s2;
(*b)["child called"];
return 0;
}
Output:
parent called
child called