How to befriend a templated class's constructor?

Posted by Kyle on Stack Overflow See other posts from Stack Overflow or by Kyle
Published on 2010-05-12T23:25:53Z Indexed on 2010/05/13 0:24 UTC
Read the original article Hit count: 535

Filed under:
|
|
|
|

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?

© Stack Overflow or respective owner

Related posts about c++

Related posts about friend