[英]In c++, when I am using multi-level pointer a lot, is it a good way to use typedef to simplify it?
In c++, when I am using multi-level pointer a lot, is it a good way to use typedef to simplify it?在c++中,当我大量使用多级指针时,使用typedef来简化是不是一个好办法?
Yes, It can be a good practice to increase code readability.是的,提高代码可读性可能是一个好习惯。
Lets state you have:让 state 你有:
class Foo
{
public:
Foo(const myotherlib::x509::certificate& cert);
private:
myotherlib::x509::certificate m_cert;
}
It is a good idea to typedef things a little bit:稍微对事物进行 typedef 是个好主意:
class Foo
{
public:
typedef myotherlib::x509::certificate cert_type;
Foo(const cert_type& cert);
private:
cert_type m_cert;
}
This increases the code readability and has the neat side effect that if you ever change the underlying certificate class, you just have to change the typedef
.这增加了代码的可读性并有一个简洁的副作用,即如果您更改基础证书 class,您只需更改
typedef
。
And to answer more completely, the same rule might apply to "multi-level" pointers: if you really are writting Foo***
all over the place it can indeed be a good idea to make things more readable, and typedef
is a good tool for that.为了更完整地回答,同样的规则可能适用于“多级”指针:如果你真的到处都写
Foo***
,那么让事情更具可读性确实是一个好主意,而typedef
是一个好主意工具。
If one day you decide to replace your Foo***
with a class that has the same semantics, the change will be trivial, not only in your code, but in any client code that uses yours as well.如果有一天您决定用具有相同语义的 class 替换您的
Foo***
,那么更改将是微不足道的,不仅在您的代码中,而且在使用您的任何客户端代码中也是如此。
Yes, it's always an good idea to have it typedef.是的,让它 typedef 总是一个好主意。 For example,
例如,
typedef int* intp;
typedef int** intpp;
Now, if your code is really organized, then you can change simply typedef to use your custom or any other smart pointer.现在,如果您的代码确实有条理,那么您可以简单地更改 typedef 以使用您的自定义或任何其他智能指针。 eg
例如
typedef auto_ptr<int> intp;
This can be a good way for debugging.这可能是调试的好方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.