So I've read through this question, and I understand the premise of dependent names in templates, and how you must sometimes qualify a method with this->
to ensure that the compiler can properly find it, however I've run into a scenario that I can't figure out how to solve. Specifically, when the object that the method belongs to is of the same type as *this
, but it's a different object (potentially of a different subclass). For example:
#include <iostream>
template<class T>
class A
{
protected:
virtual void foo() = 0;
};
template<class T>
class B : public A<T>
{
};
template<class T>
class C : public B<T>
{
protected:
void foo() override
{
std::cout << "Hello, world" << std::endl;
}
};
template<class T>
class D : public B<T>
{
protected:
void foo() override
{
B<T> *b = new C<T>();
b->foo(); // error: 'void A<T>::foo() [with T = int]' is protected
}
public:
void bar()
{
this->foo();
}
};
int main()
{
D<int> d;
d.bar();
}
Given this inheritance hierarchy, and the error when calling b->foo();
, what would be the proper way to call that function? As I understand, it should in principle be accessible to code in D
, as it's a protected member of a base class, but it's complicated by the template system.