3

Possible Duplicate:
Making a template parameter a friend?

C++ Faq 23.11 How can I set up my class so it won't be inherited from? lists the following code:

class Fred;

class FredBase {
 private:
   friend class Fred;
   FredBase() { }
};

class Fred : private virtual FredBase {
public:
   ...
};

I tried to make a generic template for the same.

#include <stdio.h>

template<typename MAKE_ME_NONINHERITABLE >
class NonInheritable{
private:
  NonInheritable(){
  }
  friend  MAKE_ME_NONINHERITABLE;  //<--- error here 
};

This give me an error:

xxx.cpp:11: error: a class-key must be used when declaring a friend

So I tried:

template<typename  MAKE_ME_NONINHERITABLE >
class NonInheritable{
private:
  NonInheritable(){
  }
  friend class MAKE_ME_NONINHERITABLE; //<--- error here 
};

class A : virtual public NonInheritable<A>{
};

And I get this error:

xxx.cpp:11: error: using typedef-name `MAKE_ME_NONINHERITABLE' after `class'

Is there a way to make this work?

Community
  • 1
  • 1
Aman Aggarwal
  • 3,905
  • 4
  • 26
  • 38
  • This approach is not realizable, sorry to be the bearer of bad news. – Jon Sep 22 '11 at 09:46
  • You will be able to use the first variant in C++11 (which changes friend stuff a bit), but so far the only compilers that seem to support it are gcc 4.7 and clang svn – PlasmaHH Sep 22 '11 at 09:47
  • @PlasmaHH MSVC accepts it as well – stijn Sep 22 '11 at 09:50

1 Answers1

3

You can use final from c++11 or sealed from microsoft extensions for c++.

Sergey Podobry
  • 7,101
  • 1
  • 41
  • 51