How to befriend a templated class's constructor?
- by Kyle
Why does
class A;
template<typename T> class B
{
private:
A* a;
public:
B();
};
class A : public B<int>
{
private:
friend B<int>::B<int>();
int x;
};
template<typename T>
B<T>::B()
{
a = new A;
a->x = 5;
}
int main() { return 0; }
result in
../src/main.cpp:15: error: invalid use of constructor as a template
../src/main.cpp:15: note: use ‘B::B’ instead of ‘B::class B’ to name the constructor in a qualified name
yet changing friend B<int>::B<int>() to friend B<int>::B() results in
../src/main.cpp:15: error: no ‘void B::B()’ member function declared in class ‘B’
while removing the template completely
class A;
class B
{
private:
A* a;
public:
B();
};
class A : public B
{
private:
friend B::B();
int x;
};
B::B()
{
a = new A;
a->x = 5;
}
int main() { return 0; }
compiles and executes just fine -- despite my IDE saying friend B::B() is invalid syntax?