In c+开发者_如何学JAVA+, when I am using multi-level pointer a lot, is it a good way to use typedef to simplify it?
Yes, It can be a good practice to increase code readability.
Lets state you have:
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:
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
.
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.
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.
Yes, it's always an good idea to have it 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. e.g.
typedef auto_ptr<int> intp;
This can be a good way for debugging.
精彩评论