开发者

Is it possible to implement the __super macro?

Please tell me if there is a way to开发者_StackOverflow中文版 manually implement the Microsoft specific __super macro...


class Base{
public:
  void func(){
    // something
  }
};

class Derived : public Base{
public:
  void func(){
    Base::func(); // just use the base class name
  }
};

Though I assume that's not what you want and you want a generic access for every class? I don't know of a direct solution, but you can always typedef your immediate base class:

class Derived : public Base{
  typedef Base super; // typedef accordingly in every class
public:
  void func(){
    super::func();
  }
};

Or even have an intermediate class just for the typedef if you really want..

template<class Base>
struct AddSuper : public Base {
protected:
  typedef Base super;
};

class Derived : public AddSuper<Base> {
public:
  void func(){
    super::func();
  }
};

Note that you should not forget to retypedef in every derived class, else you'll get holes in your call-chain.

One drawback: The whole construct blows up with multiple base classes. :/


Avoid it, it makes your code unportable. If you want to have a short name for your base class then use a typedef:

class Derived: public BaseWithALongName
{
   typedef BaseWithALongName super;
};


A keyword super was actually proposed a long while back in the C++ standardisation process,but it was rejected as unnecessary, as it could be implemented using the method described by Xeo in his answer - this is covered in Stroustrup's D&E book. I guess Microsoft decided it really is necessary, but be warned if you use it your code will not be portable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜