This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: template parameters cannot be friends


Hi Naje,

With static polymorphism, I believe you should approach the problem this way:

- - - - - - - - - - - - -
#include <iostream>

class RadioButton
{
public:
    void Draw() const
    {
        std::cout<<"radio button"<<std::endl;
    }
};

class TextButton
{
public:
    void Draw() const
    {
        std::cout<<"text button"<<std::endl;
    }
};

template<typename T>
void Draw(T const& t)
{
    t.Draw();
}

int main()
{
    RadioButton rb;
    Draw(rb);
    TextButton tb;
    Draw(tb);
}
- - - - - - - - - - - - -

You cannot have heterogenous collections of static polymorphic objects.

If you need a heterogenous collection of polymorphic objects, you need dynamic polymorphism. (And, if so, I recommend using interface classes ... classes consisting of nothing but pure virtual methods. I recommend interface classes as a rule-of-thumb.)

HTH,
--Eljay


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]