Tôi muốn nói chỉ giữ phạm vi ở mức tối thiểu; với điều đó, làm bất cứ điều gì là sạch sẽ nhất.
Nếu bạn sử dụng nó cho một hàm, hãy giữ nó trong phạm vi chức năng đó. Nếu bạn sử dụng nó cho một số chức năng, làm cho nó một typedef riêng. Và nếu bạn mong đợi người khác sử dụng nó (ngoài tiện ích, có lẽ), hãy công khai.
Trong mã:
namespace detail
{
// By convention, you aren't suppose to use things from
// this namespace, so this is effectively private to me.
typedef int* my_private_type;
}
void some_func()
{
// I am allowed to go inside detail:
detail::my_private_type x = 0;
/* ... */
}
void some_other_func()
{
// I only need the typedef for this function,
// so I put it at this scope:
typedef really::long::type<int>::why_so_long short_type;
short_type x;
/* ... */
}
typedef int integer_type; // intended for public use, not hidden
integer_type more_func()
{
return 5;
}
class some_class
{
public:
// public, intended for client use
typedef std::vector<int> int_vector;
int_vector get_vec() const;
private:
// private, only for use in this class
typedef int* int_ptr;
};
Hy vọng cung cấp cho bạn một ý tưởng về những gì tôi có ý nghĩa.
Tôi đoán tôi không hiểu tại sao bạn lại hỏi. Bạn đã nghe điều gì chưa? –
haha, uh, cái gì? Đó có phải là từ một bài hát không? – SirYakalot
Không có typedef trong ví dụ của bạn ... – interjay