0

I have the below draft of code:

  template <class A, class B = B2>
  class SM
  {
    A a;
    f();
    // .....
  };  
  template <class A, class B>
  SM<A,B>::f()
  {
    cout<< a <<" Using B"<<endl;
  };

How can I overload the behaviour of this function?

I want to get this behaviour:

 template <class A>
 SM<A,B2>::f()
 {
    cout<< a <<" Using B2"<<endl;
 };
Alex Aparin
  • 4,393
  • 5
  • 25
  • 51
  • You cannot partially specialize only one member function, have to partially specialize the whole class. Possible duplicate of http://stackoverflow.com/q/15374841/3093378 – vsoftco Jan 12 '15 at 10:31
  • But Class SM contains terible size of code. Is This the only way? – Alex Aparin Jan 12 '15 at 10:36

1 Answers1

0

You must use class B in the class SM. So you need know the type of the 'B' in SM object.

template <class A, class B = B2>
class SM
  {
    A a;
    B b;//the class B object.
    f();
    // .....
  };  
  template <class A, class B>
  SM<A,B>::f()
  {
    cout<< a <<" Using "<<typeid(b).name()<<endl;//use typeid
  };
Roger.Kang
  • 62
  • 1
  • 5
  • Bad idea. The returning value of `std::type_info::name` is not specified by standard. – ForEveR Jan 12 '15 at 10:39
  • I am sorry, but it is not right mean. I means that rather than "cout << ..." can be invoking of specific member functions of B2 – Alex Aparin Jan 12 '15 at 10:42