简体   繁体   English

我如何建立这种类型的类接口

[英]How can I build this typ of class interface

I am interested in trying something where I create a custom type and then access to its members using dot semantics. 我对尝试创建自定义类型然后使用点语义访问其成员的方法感兴趣。 For example: 例如:

 Class A{ //simplified, omitting constructors and other methods
   private:
   float numbers[3];
   public:
   float x(){ return numbers[0]; }
   float y(){ return numbers[1]; }
   float z(){ return numbers[2]; }
  }

So I can do something like this: 所以我可以做这样的事情:

  A a;
  //do stuff to populate `numbers`

  float x=a.x;

But I would also like to make the elements in numbers lvalues so I can do something like this: 但是我也想使numbers的元素成为左值,所以我可以做这样的事情:

  A a;
  a.y=5; //assigns 5 to numbers[1]

How can I do this setting method? 如何设置此方法?

First. 第一。 You made functions x, y and z but assigning them to float. 您制作了函数 x,y和z,但将它们分配为float。 This wouldn't work. 这行不通。 Second. 第二。 Change these functions to return referencies: 更改这些函数以返回引用:

class A{ //simplified, omitting constructors and other methods
   private:
   float numbers[3];
   public:
   float & x(){ return numbers[0]; }
   float & y(){ return numbers[1]; }
   float & z(){ return numbers[2]; }
};
...
A point;
float x = point.x();
point.x() = 42.0f;

There's another way: declare referencies as a members of class and initialize them in c-tor: 还有另一种方式:将引用声明为类的成员,然后在c-tor中对其进行初始化:

class A{ //simplified, omitting constructors and other methods
   private:
   float numbers[3];
   public:
   float & x;
   float & y;
   float & z;
   A() : x( numbers[ 0 ] ), y( numbers[ 1 ] ), z( numbers[ 2 ] ) {}
};
...
A point;
float x = point.x;
point.x = 42.0f;

PS Pay an attention on comment, that gave @MikeSeymour PS注意评论,这给了@MikeSeymour

You can return a reference to allow assignment: 您可以返回引用以允许分配:

float & x(){ return numbers[0]; }
      ^

// usage
A a;
a.x() = 42;

You should also have a const overload, to allow read-only access to a const object: 您还应该具有const重载,以允许对const对象的只读访问:

float x() const {return numbers[0];}
          ^^^^^

// usage
A const a = something();
float x = a.x();

Not unless you actually have public variables named x, y and z. 除非您实际上有名为x,y和z的公共变量。

Or you can return a reference and then do ay() = 5 或者您可以返回引用,然后执行ay() = 5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在接口类中创建超类对象? - how can I create an superclass object within interface class? 如何向模板类公开统一接口? - How can I expose a uniform interface to a template class? 如何将C ++类与Java连接? - How can I interface a C++ class to Java? 如何将基 class 的实现用于接口 class 的纯虚方法? - How can I use the implementation of a base class for a pure virtual method of a interface class? 我的班级如何继承接口和QObject? - How can my class inherit an interface and QObject ? 将 crtp 与通用接口类一起使用时,如何避免虚拟调用开销? - How can I avoid virtual call overhead when using crtp with common interface class? 如何不使用类而应对不同的返回类型,以保留API接口 - How can I cope with different return types without templating a class so as to preserve API interface C ++-如何将类上的所有成员函数调用委派给同一接口的成员 - C++ - How can I delegate all member functions invocations on a class to a member of the same interface 如何为类的每个接口分别定义一些功能? - How can I define some functions individually for each interface of my class? 如何解决C ++ Builder中的接口方法和基类方法名称冲突? - How can I resolve interface method and base class method name conflict in C++ Builder?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM