简体   繁体   English

typedef boost :: shared_ptr <MyJob> PTR; 或#define Ptr boost :: shared_ptr

[英]typedef boost::shared_ptr<MyJob> Ptr; or #define Ptr boost::shared_ptr

I've just started working on a new codebase where each class contains a shared_ptr typedef ( similar to this ) like: 我刚刚开始研究一个新的代码库,其中每个类都包含一个shared_ptr typedef( 类似于这个 ),如:

typedef boost::shared_ptr<MyClass> Ptr;

Is the only purpose to save typing boost::shared_ptr? 是保存输入boost :: shared_ptr的唯一目的吗?

If that is the case, is the only reason not to do 如果是这样的话,唯一的理由是不这样做

#define Ptr boost::shared_ptr 

in one common header the general problems with #define? 在一个常见的标题中#define的一般问题? Then you can do: 然后你可以这样做:

Ptr<MyClass> myClass(new MyClass);

which is no more typing than 这不是打字比

MyClass::Ptr myClass(new MyClass);

and saves the Ptr definition in each class. 并在每个类中保存Ptr定义。

A macro (#define) is always defined globally. 始终全局定义宏(#define)。 This means that every use of the 'string' Ptr (even a variable) will be replaced by the macro. 这意味着每次使用'string'Ptr(甚至变量)都将被宏替换。

The typedef can be placed in a class, in a namespace, ... so you have much better control over it. typedef可以放在一个类中,在命名空间中......所以你可以更好地控制它。

EDIT: another advantage is that you can haver different Ptr types in different classes, eg 编辑:另一个优点是你可以在不同的类中使用不同的Ptr类型,例如

  • ClassX::Ptr is a boost shared_ptr ClassX :: Ptr是一个boost shared_ptr
  • ClassY::Ptr can be another shared_ptr ClassY :: Ptr可以是另一个shared_ptr
  • ClassZ::Ptr can be a simple "Class Z *" pointer ClassZ :: Ptr可以是一个简单的“Class Z *”指针

If these classes are then used in templated code, you can use T::Ptr as a type of pointer to the class, and the template will use the most-appropriate pointer for the class. 如果然后在模板化代码中使用这些类,则可以使用T :: Ptr作为指向类的指针类型,并且模板将使用最适合该类的指针。

Disadvantages of defines have been discussed extensively all over the web. 已经在整个网络上广泛讨论了定义的缺点。 For example, it will collide with Ptr in another namespace: 例如,它将与另一个命名空间中的Ptr冲突:

someLibrary::Ptr somethingElse -> somelibrary::crap

If typing boost::shared_ptr really annoys, you can be using namespace boost . 如果输入boost :: shared_ptr真的很烦,你可以using namespace boost It will preserve the readability (people really want to know it's boost shared_ptr). 它将保持可读性(人们真的想知道它的提升shared_ptr)。

Another thing I can suggest you is a series of typedefs. 我可以建议你的另一件事是一系列的typedef。 In my company, there's a convention that MyClassPtr is a typedef to boost::shared_ptr. 在我的公司中,有一个约定MyClassPtr是boost :: shared_ptr的typedef。

Is the only purpose to save typing boost::shared_ptr? 是保存输入boost :: shared_ptr的唯一目的吗?

Yes, pretty much. 是的,差不多。 Well, not to save typing per se, it's to improve readability . 好吧,不是为了节省打字本身,而是为了提高可读性 But I think that's kind of what you meant. 但我认为这就是你的意思。

Compare these and see which you like. 比较这些,看看你喜欢哪个。 There's no correct answer, other than to be aware of problems with macros and namespace clutter. 除了了解宏和命名空间混乱的问题之外,没有正确的答案。

boost::shared_ptr<Foo> func (boost::shared_ptr<Foo> a, boost::shared_ptr<Foo> b);

shared_ptr<Foo> func (shared_ptr<Foo> a, shared_ptr<Foo> b);

Ptr<Foo> func (Ptr<Foo> a, Ptr<Foo> b);

Foo::ptr func (Foo::ptr a, Foo::ptr b);

FooPtr func (FooPtr a, FooPtr b);

Foo* func (Foo* a, Foo* b);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM