2016-06-08 65 views

回答

3

成員類型定義矢量對象使用的類型。許多標準容器使用成員類型來描述所使用的類型,因此程序員不需要手動找出它們。這在處理複雜模板時尤其有用,其中類型可能難以確定。

例如,返回類型std::vector<___>::size()通常是std::size_t,但是另一個C++向量實現可能返回不同的整數類型(例如int32_t)。不用假設代碼中需要什麼類型(並且可能引入危險的轉換),您可以簡單地使用向量公開的成員類型,每次都用完美的類型編寫代碼。例如:

std::vector<int> vec; 

const std::vector<int>::value_type val = 4; 
// std::vector<int>::value_type is a typedef of int! 

vec.push_back(val); 

const std::vector<int>::size_type size = vec.size(); 
// std::vector<int>::size_type is a typedef of std::size_t, usually. 

const std::size_t size2 = vec.size(); 
// same as above, but we assume that vec.size() returns a size_t. 
// If it does not, we may cause a narrowing conversion! 

for (std::vector<int>::const_iterator it = vec.begin(), end = vec.end(); it != end; ++it) 
{ 
    // The const_iterator type is also a member type of vector. 
    std::cout << *it << std::endl; 
} 

迭代器可能是標準容器中最常用的成員類型。我們可以使用容器公開的iterator成員類型,而不是必須弄清楚迭代器是否是隨機訪問迭代器,簡單前向迭代器或其他任何類型的迭代器。

C++ 11 auto關鍵字可以更進一步。而不是做:

const std::vector<int>::size_type size = vec.size(); 

我們現在只是做:

const auto size = vec.size(); 

,編譯器會自動工作了。

一般來說大多數C++標準對象將使用相同的部件類型,其中可能的(例如size_tsize_typeTvalue_typeT&reference_type),但它不能保證(iterator成員類型爲std::vectorstd::list不同,因爲他們的實現是非常不同的,他們不能使用相同的迭代器類型)。

3

下面是一個在C典型的類++:

struct Foo 
{ 
    int n;       // non-static data member 
    static const float x;   // static data member 

    int f() const;     // non-static member function 
    static int g();    // static member function 

    template <typename T> 
    void h(T);      // non-static member function template 

    struct X { bool a; };   // member type 
    template <typename> struct Q; // member class template 

    using T = Q<int>;    // member type (member typedef) 
}; 

用法示例:

// Use static members 

print(Foo::x); 
print(Foo::g()); 

Foo::X y; 
Foo::Q<double> z; 
Foo::T w; 

// Use non-static members (requires instance) 

void demo(const Foo & a) 
{ 
    print(a.n); 
    print(a.f()); 
    print(a.h<float>()); 
} 
1

你鏈表 「VALUE_TYPE T」 的 「成員類型」 頁面。這是屬於或者是T的成員考慮

using VEC = std::vector<double>; 
VEC dv { 4.2 }; 

現在讓我們說,我們要存儲從dv值,但我們不希望硬編碼類型的類型定義,以便將來的變化到定義VEC將做正確的事(TM)。

using VEC = std::vector<double>; 
VEC dv { 4.2 }; 
// ...  
VEC::value_type d = dv.front(); 

或者,如果你想要編寫跨平臺的移植代碼,它可以幫助使用「SIZE_TYPE」,以確保您存儲有足夠大的尺寸:

int s = dv.size(); // wrong on 64-bit system 
VEC::size_type s = dv.size(); // always correct 

也就是說, std::vector<double>擁有一個類型定義,value_type,該值定義爲向量中的'T'類型,或者在我們的例子中爲double。

這是泛型編程大多是重要的:

template<typename T> 
void dothing(const T& container) 
{ 
    T::const_pointer* c = &container; // for some reason I need it's address 
    // .. other stuff 
} 
+0

您必須小心使用'&'來獲取泛型代碼中變量的地址。某些類型可能會覆蓋'operator&',因此您的示例會失敗。爲了得到真正的地址而不管運算符是否被覆蓋,使用['std :: addressof()'](http://en.cppreference.com/w/cpp/memory/addressof)來代替,例如:'c = std :: addressof(container);' –

0

有你需要明白兩兩件事:

  1. 您可以爲類型的別名。

例如,

using blah = int; 

將使blahint的別名。換句話說,你可以在任何地方使用blah而不是int

(還有另一種方法,使用typedef代替using一個類型別名,但它是更少可讀的。)

  • 你可以把這些別名的類中。
  • 例如,如果你申報了下面的結構

    struct S 
    { 
        using type = int; 
    }; 
    

    你將能夠使用S::type,而不是一個int。

    類內部的這種別名通常被稱爲成員類型

    相關問題