C++ 访问C+中的类型成员+;

C++ 访问C+中的类型成员+;,c++,C++,给定一个容器,如向量 解决方法是使用decltype,如下所示: int h(decltype(v)::iterator it) { return *it; } 但这种方法在课堂上甚至不起作用,因为以下方法失败了: class A { public: int h(decltype(x)::iterator it) { return *it; } private: vector<int> x; }; 基本上,C++通过使用::< /代

给定一个容器,如
向量

解决方法是使用
decltype
,如下所示:

int h(decltype(v)::iterator it) {
    return *it;
}
但这种方法在课堂上甚至不起作用,因为以下方法失败了:

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

基本上,C++通过使用<代码>::< /代码>,可以获得值或类型。所以
MyType::AnotherType
MyType::AValue
都很好。当您使用
查看实例时,这只意味着它希望解析一个符号,该符号是一种值(字段、func等)。希望这能有所帮助。

正如@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;
    }
};

也没有C++中的“类型成员”(至少我在C++标准中找不到)。在类中声明的类、枚举和Type声明都称为“嵌套类型”或“嵌套类”。

类型和对象在C++中是并行的(主要是编译时间,其他大部分是运行时)。DOT应用于对象,因此应该在运行时工作,因此不适合类型。作为旁注,您可以在这里使用
auto
来保存一些输入。@FrançoisAndrieux对不起,我指的是最后一个示例
a::h
的函数参数中的
auto
如何工作?对我来说,它不是用c++17编译的。
decltype()
不是一种解决方法,而是一种方法,它在类中工作,使用它之前只需定义该变量。
return*It++*(+++It) UB的伟大例子:问题不在于C++正在做什么,而是为什么。类型成员是一个成员。有趣的例子,但奇怪的是它没有在CLANG中编译:并在gcc工作。我觉得这个程序无效,但找不到原因。
class A
{
public:
    int h(decltype(x)::iterator it) {
        return *it;
    }
private:
    vector<int> x;
};
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)
}
#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;
    }
};
#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;
}