0

and first of all, thanks to any people that can help me. I am currently working on a XML parser. Here is what I had not so long ago :

class Root
{
    // bla bla
}

The Root is the first item of a linked list. It has a pointer to the first item, and function to add and remove item from the list.

class Base
{
    // bla bla
}

The Base class is an simple Item, with a Pointer to the next item.

class Child : public Base
{
    // bla bla
};

The Child class is derived from the Base class, and contains data (such has name and values for the elements and the attributes of my XML file).

class Derived_1 : public Child
{
    // bla bla
};

class Derived_2 : public Child
{
    // bla bla
};

The Derived class are my Attributes and my Elements. the Elements contains Root for a list of Attributes, and another for a list of elements (so i can build a hierarchy).

My base class need to contain the function of the Root, but can't inherit them as a child. So we "friend" the Root function in the Base class.

class Base
{
    friend class Root
    // bla bla
}

Recently, we spoke about the templates, and had to integrate them in the XML parser. So, first, we transformed the Root class in a template. As a consequence, The Root being used for the two Derived class, the Base class was affected this way :

template< class T >
class Root
{
    // bla bla
}

-------------

class Derived_1;
class Derived_2;
class Child;

class Base
{
    friend class Root<Derived_1>
    friend class Root<Derived_2>
    friend class Root<Child>

    // bla bla
}

-------------

class Child : public Base
{
    // bla bla
};

Until now, no problem. We had to forward declare the Derived class to avoid a loop in the includes. Then, we had to make the Base class a template, so we had :

class Derived_1;
class Derived_2;
class Child;

template< class T >
class Base
{
    friend class Root<Derived_1>
    friend class Root<Derived_2>
    friend class Root<Child>

    // bla bla
}

-------------

class Child : public Base<Child>
{
    // bla bla
};

And then, the problem showed up, we had the make the Child class a template, so it became :

template< class T >
class Child : public Base<T>
{
    // bla bla
};

However, the forward declaration in the Base class is not good anymore, and the way we declare friend class neither. I have to find a way to forward declare the Child class, but I can't see how. Until now, i didn't have any difficulties with it, but here, I don't see the logic behind it. Can someone please explain to me the way things work here (I do not ask for an answer were I just have to copy/past what you tell me, but I want to understand what's going on. thanks a lot for any help.

Ellenack
  • 43
  • 1
  • 4
  • possible duplicate of [What is the curiously recurring template pattern (CRTP)?](http://stackoverflow.com/questions/4173254/what-is-the-curiously-recurring-template-pattern-crtp) – πάντα ῥεῖ May 16 '14 at 21:12

1 Answers1

2

Your difficulties seem to stem from the difference between a class and a class template. In particular, in order to friend a forward declared type that's instantiated from a class template, you need to forward declare the class template:

template <class T> class Child;

Then you can friend it:

template <class T> class Base
{
    friend class Child<T>; // or
    friend class Root< Child<T> >;
    : : :
};

But do note that friendship is the tightest form of coupling, and should be avoided when there are better means to achieve your ends.

Community
  • 1
  • 1
Michael Urman
  • 15,737
  • 2
  • 28
  • 44
  • Ooooh, ok, I was searching waaaaay to far. I was trying to forward declare The Child class this way : "template class Child;", along with the two Derived class, and in the Base Class, I was "friending" them (can't find a better word) this way : "friend class Root>;". In the end, it was "easier" than what I was looking for. I will have to look deeper in all this when I have the time. Thanks a lot. After fixing a few more issues showing up apter fixing this one, their was no more problem. Thanks again. :) – Ellenack May 16 '14 at 21:41