“转换”函数参数(“Transforming” Function Arguments)
我正在编写与库接口的C ++类。 该库有许多函数,如下所示:
Library.h
int FunctionA(int deviceNumber, ...); int FunctionB(int deviceNumber, ...); int FunctionC(int deviceNumber, ...); int FunctionD(int deviceNumber, ...); int FunctionE(int deviceNumber, ...);
我的C ++类的每个实例都有一个永远不会改变的关联
deviceNumber
,所以我将deviceNumber
存储为成员变量,每次调用库函数时,我都将成员作为函数调用的第一个参数传递。这没关系,我没有理由改变它的方式。 但出于好奇,我想知道C ++是否有任何“转换”参数的机制可以让我避免在每个调用中传递相同的参数。 实现这一目标的显而易见的方法是超载一切。 假设我的班级叫做
Foo
:Foo.cpp中
int Foo::FunctionA(...) { // deviceNumber_ is a const member return ::FunctionA(deviceNumber_, ...); }
唯一的问题是这需要每个函数调用的方法,因此随着库的增长,没有代码生成器它会变得越来越烦人。
是否有任何通用的方法来提供重载行为而不实际重载函数? C ++中是否有一种机制可以将参数“扩展”为多个参数? 我想象它看起来像:
// These two calls are equivalent FunctionA(deviceNumber, ...); FunctionA(Magic(...)); // Magic() expands the arguments, adding deviceNumber
即使解决方案比单独留下一切更加丑陋和可读性,我很好奇是否可能。 搜索后,可变参数模板似乎是最接近的匹配,但我无法真正地了解如何使用它们来完成此任务。
I'm writing C++ class that interfaces with a library. The library has a bunch of functions that look like the following:
Library.h
int FunctionA(int deviceNumber, ...); int FunctionB(int deviceNumber, ...); int FunctionC(int deviceNumber, ...); int FunctionD(int deviceNumber, ...); int FunctionE(int deviceNumber, ...);
Each instance of my C++ class has an associated
deviceNumber
which never changes, so I havedeviceNumber
stored as a member variable, and every time I call a library function, I pass the member in as the function call's first argument.This is fine, and there's no real reason for me to change the way it is. But out of curiosity, I was wondering if C++ had any mechanism to "transform" arguments that would let me avoid passing the same argument in every call. The obvious way to accomplish this is to overload everything. Let's say my class is called
Foo
:Foo.cpp
int Foo::FunctionA(...) { // deviceNumber_ is a const member return ::FunctionA(deviceNumber_, ...); }
The only problem is that this requires a method for each function call, so as the library grows, it gets more and more annoying without a code generator.
Is there any general way to provide the overloading behaviour without actually overloading the functions? Is there a mechanism in C++ to "expand" an argument into multiple arguments? I'm imagining it would look like:
// These two calls are equivalent FunctionA(deviceNumber, ...); FunctionA(Magic(...)); // Magic() expands the arguments, adding deviceNumber
Even if the solution is much uglier and less readable than leaving everything alone, I'm curious if it's possible. After searching around, variadic templates seem to be the closest match, but I can't really wrap my head around how they could be used to accomplish this.
原文:https://stackoverflow.com/questions/28909656
最满意答案
最简单的方法是覆盖类的
toString()
方法,该方法将您放入JCombo
模型中的实例。 这样你就可以获得每个项目的“好名声”。
当然,该课程应该包含每个项目所需的一切,例如id和name。 在选择更改时,您可以使用所选项目的ID。
如果你不能覆盖'toString()'或者想要从表示对象中分离你已经拥有的对象(例如,如果它们是DTO),那么只用你需要的东西创建你自己的类。public class User { private int id; private String name; public User(int id, String name) { this.id = id; this.name = name; } public int getId() { return this.id; } public String getName() { return this.name; } public String toString() { return this.getName(); } }
Easiest way for you would be to override
toString()
method of the class which instances you are putting in theJCombo
's model. That way you get your 'nice name' for each item.
That class, of course, should contain everything you need for each item, e.g. id and name. On selection change, you can than use the id of the selected item.
If you cannot override 'toString()' or you want to separate objects you already have (e.g. if they are DTO) from the presentation objects, create your own class with only the things you need.public class User { private int id; private String name; public User(int id, String name) { this.id = id; this.name = name; } public int getId() { return this.id; } public String getName() { return this.name; } public String toString() { return this.getName(); } }
相关问答
更多-
TCP/IP模型是一个________。[2023-05-19]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
JComboBox的问题(Problems with JComboBox)[2023-12-26]
那是因为JComboBox使用equals来检查项目是否相等。 在您的情况下,这两个String是相等的,因此它返回匹配的第一个索引。 如果你真的需要这样做,你可能需要像这样定义自己的项类: private static class MyItem { private String value; public MyItem(String value) { this.value = value; } public String getValue() { ... -
JComboBox + JPA(JComboBox + JPA)[2022-12-18]
组合框接受任何类型的对象。 为了显示它们的值,它使用对象的toString()方法。 所以,你可能有这样的代码: @Entity public class Genre { // fields and methods @Override public String toString() { return this.name; } } // in your GUI Listgenres = findAllGenresSortedByName( ... -
最简单的方法是覆盖类的toString()方法,该方法将您放入JCombo模型中的实例。 这样你就可以获得每个项目的“好名声”。 当然,该课程应该包含每个项目所需的一切,例如id和name。 在选择更改时,您可以使用所选项目的ID。 如果你不能覆盖'toString()'或者想要从表示对象中分离你已经拥有的对象(例如,如果它们是DTO),那么只用你需要的东西创建你自己的类。 public class User { private int id; private String name; ...
-
根问题似乎与您实例化InventoryTrackingSystem的两个实例有关。 设置一个实例中的cb字段,而另一个实例中的相同字段仍为空。 我不确定你为什么要设置这个类的两个实例,但我怀疑你是否检查过你会发现你的问题。 祝你好运。 The root problem appears to be related to the fact that you instantiate two instances of your InventoryTrackingSystem. The cb field in one ...
-
我如何声明JComboBox?(How do I declare a JComboBox?)[2023-07-29]
@Andy提到的问题是,你的课堂名称和挥杆组件有碰撞。 如果您绝对必须命名您的类JComboBox,则必须按照完全限定的名称引用swing组件,因此 public class JComboBox extends JFrame { private javax.swing.JComboBox box; 如果将鼠标悬停在您的私有实例上,则应该看到与您创建JComboBox类的包相匹配的完全限定名。 节省一些痛苦并重新命名你的班级。 Your problem, as @Andy mentioned is ... -
JComboBox box=new JComboBox(values); TableColumn col = table.getColumnModel().getColumn(0); col.setCellEditor(new DefaultCellEditor(box)); JComboBox box=new JComboBox(values); TableColumn col = table.getColumnModel().getColumn(0); col. ...
-
来自swingx的JComboBox(JComboBox from swingx)[2023-06-05]
SwingX组件扩展了它们的JComponent对应物,可以像任何其他JComponent一样添加(比如使用addComponent(....)方法)。 如果您使用的是NetBeans IDE 7.0(或更早版本),则可以将JXComboBox的类复制到正在使用的包中,然后拖放,就像从调色板添加一样。 SwingX components extend their JComponent counterparts and can be added as any other JComponent (like us ... -
尝试这个,应该工作。 你必须覆盖setBackground ...因为,内部机制使用当前外观的默认颜色: Color[] colors={Color.white,Color.red,Color.blue,Color.green}; JComboBox colorBox = new JComboBox(colors); colorBox.setMaximumRowCount(5); colorBox.setPreferredSize(new Dimension(50,20)); colorBox.setRen ...