在 c++ 中访问 getter setter 内部的结构变量班级

发布于 2024-08-09 17:06:53 字数 530 浏览 2 评论 0原文

好的,我在 C++ 中有类似的东西:

class MyClass{
private:
  int someVariable;
  int someOtherVariable;

  struct structName{
    int someStructVariable;
    int someOtherStructVariable;
  };//end of struct

public:
  //getters & setters for the defined variables.

  int getSomeStructVariable()
  {
    // this does not work I get this error: "error: expected primary-expression
    // before '.' token"
    return structName.someStructVariable;
  } 
};//end of class

在这种情况下我应该如何编写我的 getter 或 setter?

Okay, I have something like this in C++:

class MyClass{
private:
  int someVariable;
  int someOtherVariable;

  struct structName{
    int someStructVariable;
    int someOtherStructVariable;
  };//end of struct

public:
  //getters & setters for the defined variables.

  int getSomeStructVariable()
  {
    // this does not work I get this error: "error: expected primary-expression
    // before '.' token"
    return structName.someStructVariable;
  } 
};//end of class

How should I write my getter or setter in this case?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

所谓喜欢 2024-08-16 17:06:53

structName 是类型名称的一部分,而不是变量名称的一部分。您需要给它一个名称,例如:

struct structName {
  int someStructVariable;
  int someOtherStructVariable;
} myStructure;

然后在您的访问器中使用:

return myStructure.someStructVariable;

这应该会得到您想要的结果。结构变量的其他替代方法是将结构定义与变量声明分开:

struct structName {
  int someStructVariable;
  int someOtherStructVariable;
};

struct structName myStructure;

或者添加到 typedef 中:

typedef struct structName {
  int someStructVariable;
  int someOtherStructVariable;
} structTypedefName;

structTypedefName myStructure;

structName is part of the type name, not the variable name. You need to give it a name, something like:

struct structName {
  int someStructVariable;
  int someOtherStructVariable;
} myStructure;

And then in your accessor use:

return myStructure.someStructVariable;

That should get you the result you want. Other alternatives for the structure variable are to separate out the structure definition from the variable declaration:

struct structName {
  int someStructVariable;
  int someOtherStructVariable;
};

struct structName myStructure;

or to add in typedef:

typedef struct structName {
  int someStructVariable;
  int someOtherStructVariable;
} structTypedefName;

structTypedefName myStructure;
千寻… 2024-08-16 17:06:53
struct A {
  A() : _n(0) {}
  int get_n() const {
    return _n;
  }
  void calculate(int a) {
    _n = a * a * a;
  }
private:
  int _n;
};

这是一个完整的例子。如果您想要一个镜像 set_n 而不是操纵数据的东西,那么您可能应该删除 getter/setter(因为您会错误地使用它们)并将数据成员公开。

另外,请记住:使用 struct 定义类与使用 class 定义类的工作方式相同,但有一个例外:public 而不是 private 作为默认访问权限成员和基地。

struct A {
  A() : _n(0) {}
  int get_n() const {
    return _n;
  }
  void calculate(int a) {
    _n = a * a * a;
  }
private:
  int _n;
};

Here's a complete example. If you want a mirror set_n instead of something which manipulates the data, then you should probably drop the getter/setter (as you'd be using them incorrectly) and make the data member public.

Also, remember: defining classes with struct works identically to defining classes with class but for one exception: public instead of private as the default access for members and bases.

沙与沫 2024-08-16 17:06:53

为每个结构体字段编写 getter/setter 并不是一个好主意。更好的解决方案是:

typedef struct {
    int field1;
    int field2;
} structType;

class MyClass {
private:
    structType _structName;
public:
    const structType & getStructName() const {
        return _structName;
    }
}

structTypeMyClass 分开。将 getter 用作:

MyClass m;
int var;
var = m.getStructName().field1;

需要第一个 const 来返回 const 值。第二个是 const- Correctness 所必需的。

有两种方法可以定义 setter:

class MyClass {
// ...
public:
    // type 1
    void setStructName(const structType &s) {
        _structName = s;
    }
    // type 2
    structType & setStructName() {
        return _structName;
    }
}

使用第一种方法:

MyClass m;
structType s;
m.setStructName(s);

第二个版本还允许您修改单独的结构字段:

m.setStructName() = s;
m.setStructName().field1 = 10;

请注意,如果结构包含指针,则 setter 可能需要一些调整。

It is not a good idea to write a getter/setter for each structure field. A better solution is:

typedef struct {
    int field1;
    int field2;
} structType;

class MyClass {
private:
    structType _structName;
public:
    const structType & getStructName() const {
        return _structName;
    }
}

Keep structType and MyClass separate. Use the getter as:

MyClass m;
int var;
var = m.getStructName().field1;

The first const is needed to return a const value. The second one is needed for const-correctness.

There are two ways to define a setter:

class MyClass {
// ...
public:
    // type 1
    void setStructName(const structType &s) {
        _structName = s;
    }
    // type 2
    structType & setStructName() {
        return _structName;
    }
}

Use the first one as:

MyClass m;
structType s;
m.setStructName(s);

The second version enables you to modify separate structure fields as well:

m.setStructName() = s;
m.setStructName().field1 = 10;

Note that the setter might need some adjustments if the structure contains pointers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文