代码之家  ›  专栏  ›  技术社区  ›  Araeos

C++中访问类型成员

c++
  •  0
  • Araeos  · 技术社区  · 7 年前

    vector<int>

    #include <vector>
    using namespace std;
    vector<int> v{1, 2, 3};
    

    为什么访问诸如 iterator const_iterator :: 指定范围,但是否有理由禁止 v.const_iterator 什么时候 v 知道了吗? 例子:

    int f(v.iterator it) {
        return *it;
    }
    // or
    int g(v::iterator it) {
        return *it;
    }
    

    decltype 如所示:

    int h(decltype(v)::iterator it) {
        return *it;
    }
    

    但这种方法在类中甚至不起作用,因为以下操作失败:

    class A
    {
    public:
        int h(decltype(x)::iterator it) {
            return *it;
        }
    private:
        vector<int> x;
    };
    

    只是一点小贴士。 如前所述 v.iterator 在使用点(编译时)忽略运行时多态性。但对于静态类成员也是如此。

    struct A
    {
        static const int x = 1;
    };
    struct B : public A
    {
        static const int x = 2;
    };
    void eval()
    {
        B b;
        A& ar = b;
        b.x; // 2
        ar.x; // 1, even though ar refers to the same underlying object (by the base type)
    }
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   KamilCuk    7 年前

    正如@Slava在评论中指出的, decltype(x)

    #include <vector>
    using namespace std;
    vector<int> v{1, 2, 3};
    int f(decltype(v)::iterator it) {
        return *it;
    }
    
    int g(decltype(v)::iterator it) {
        return *it;
    }
    
    class A
    {
    private:
        vector<int> x;
    public:
        int h(decltype(x)::iterator it) {
            return *it;
        }
    };
    

    成员访问 . 运算符和范围解析运算符 :: 不能超载。你可以从名字中推断出, 习惯了 access members ,而 用于访问 scope

    #include <iostream>
    
    struct B {
        class iterator { };
    
        // no need for typename, compiler knows that we mean typedef B::iterator, as he can only find it
        iterator iterator1;
    
        // member named the same as class, ops!
        int iterator;
    
        // we need to use typename here, B::iterator is resolved as member
        // iterator iteartor3;
        typename B::iterator iterator2;
    };
    
    int main() {
        B bobj;
    
        // we access the member iterator inside b
        bobj.iterator = 1;
    
        // we declare object of B::iterator type
        // we need to tell compiler that we want only types
        typename B::iterator iterator;
    
        // this will work too
        typename decltype(bobj)::iterator iterator2;
    
        // we declare a member pointer to the iterator member inside some B class
        // no typename, as I want pointer to member, not pointer to... type
        int B::* pointer = &B::iterator;
    
        // this is just a pointer to the iterator specifically in bobj class
        int * pointer2 = &bobj.iterator;
    
        // foo(bar) 
        bobj.*pointer = 1;
    
        // this will work as expected
        int decltype(bobj)::* pointer3 = &B::iterator;
    }
    

    另外,C++中没有“类型成员”(至少我在C++标准中找不到它们)。在类中声明为成员的类、枚举和typedefs声明称为“嵌套类型”或“嵌套类”。

        2
  •  0
  •   mister why    7 年前

    基本上,C++允许您在访问它们时获得值或类型。 :: . 所以 MyType::AnotherType 也很好 MyType::AValue .