C++:从指针到类访问成员结构的语法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/915106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:55:42  来源:igfitidea点击:

C++: syntax for accessing member struct from pointer to class

c++structmember

提问by Peter ?lsted

I'm trying to access a member structs variables, but I can't seem to get the syntax right. The two compile errors pr. access are: error C2274: 'function-style cast' : illegal as right side of '.' operator error C2228: left of '.otherdata' must have class/struct/union I have tried various changes, but none successful.

我正在尝试访问成员结构变量,但我似乎无法获得正确的语法。两个编译错误pr。访问是:错误 C2274:“函数式强制转换”:“.”的右侧是非法的。操作员错误 C2228:'.otherdata' 的左边必须有类/结构/联合我尝试了各种更改,但都没有成功。

#include <iostream>

using std::cout;

class Foo{
public:
    struct Bar{
        int otherdata;
    };
    int somedata;
};

int main(){
    Foo foo;
    foo.Bar.otherdata = 5;

    cout << foo.Bar.otherdata;

    return 0;
}

回答by schnaader

You only define a struct there, not allocate one. Try this:

你只在那里定义一个结构,而不是分配一个。尝试这个:

class Foo{
public:
    struct Bar{
        int otherdata;
    } mybar;
    int somedata;
};

int main(){
    Foo foo;
    foo.mybar.otherdata = 5;

    cout << foo.mybar.otherdata;

    return 0;
}

If you want to reuse the struct in other classes, you can also define the struct outside:

如果要在其他类中重用该结构体,也可以在外部定义该结构体:

struct Bar {
  int otherdata;
};

class Foo {
public:
    Bar mybar;
    int somedata;
}

回答by aJ.

Baris inner structure defined inside Foo. Creation of Fooobject does not implicitly create the Bar's members. You need to explicitly create the object of Bar using Foo::Barsyntax.

Bar是内部定义的内部结构Foo。创作Foo对象不隐式创建Bar的成员。您需要使用Foo::Bar语法显式创建 Bar 的对象。

Foo foo;
Foo::Bar fooBar;
fooBar.otherdata = 5;
cout << fooBar.otherdata;

Otherwise,

除此以外,

Create the Bar instance as member in Fooclass.

创建 Bar 实例作为Foo类中的成员。

class Foo{
public:
    struct Bar{
        int otherdata;
    };
    int somedata;
    Bar myBar;  //Now, Foo has Bar's instance as member

};

 Foo foo;
 foo.myBar.otherdata = 5;

回答by Peter ?lsted

You create a nested structure, but you never create any instances of it within the class. You need to say something like:

您创建了一个嵌套结构,但您从未在类中创建它的任何实例。你需要这样说:

class Foo{
public:
    struct Bar{
        int otherdata;
    };
    Bar bar;
    int somedata;
};

You can then say:

然后你可以说:

foo.bar.otherdata = 5;

回答by stefanB

You are only declaring Foo::Bar but you don't instantiate it (not sure if that's the correct terminology)

你只是在声明 Foo::Bar 但你没有实例化它(不确定这是否是正确的术语)

See here for usage:

用法见这里:

#include <iostream>

using namespace std;

class Foo
{
    public:
    struct Bar
    {
        int otherdata;
    };
    Bar bar;
    int somedata;
};

int main(){
    Foo::Bar bar;
    bar.otherdata = 6;
    cout << bar.otherdata << endl;

    Foo foo;
    //foo.Bar.otherdata = 5;
    foo.bar.otherdata = 5;

    //cout << foo.Bar.otherdata;
    cout << foo.bar.otherdata << endl;

    return 0;
}

回答by Naveen

struct Bar{
        int otherdata;
    };

Here you have just defined a structure but not created any object of it. Hence when you say foo.Bar.otherdata = 5;it is compiler error. Create a object of struct Bar like Bar m_bar;and then use Foo.m_bar.otherdata = 5;

在这里,您刚刚定义了一个结构,但没有创建它的任何对象。因此,当您说foo.Bar.otherdata = 5;这是编译器错误时。创建一个 struct Bar 对象,Bar m_bar;然后使用Foo.m_bar.otherdata = 5;