C++ typedef class use

后端 未结 2 994
余生分开走
余生分开走 2021-02-04 06:56

Why use a typedef class {} Name ?

I learnt this in IBM C++ doc, no hint to use here.

2条回答
  •  清歌不尽
    2021-02-04 07:18

    This answer assumes that there's some interesting content in the class, not just {}.

    In C++, you can have a function with the same name as a class (for compatibility with C), but you pretty much never want to.

    You can't have a function with the same name as a typedef, so doing this protects you against ill-disciplined name choices. Pretty much nobody bothers, and even if you're going to bother you'd probably write it:

    class Name {};
    typedef Name Name; // reserve the name
    

    If the code you're referring to really is as written (I can't see it by following your link), then it's rather like class Name {}; (which is a peculiar thing to write, why would you call an empty class Name?), but modified for the above consideration.

提交回复
热议问题