首页 \ 问答 \ “转换”函数参数(“Transforming” Function Arguments)

“转换”函数参数(“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 have deviceNumber 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
更新时间:2022-03-08 18:03

最满意答案

最简单的方法是覆盖类的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 the JCombo'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();
    }
}

相关问答

更多
  • 那是因为JComboBox使用equals来检查项目是否相等。 在您的情况下,这两个String是相等的,因此它返回匹配的第一个索引。 如果你真的需要这样做,你可能需要像这样定义自己的项类: private static class MyItem { private String value; public MyItem(String value) { this.value = value; } public String getValue() { ...
  • 组合框接受任何类型的对象。 为了显示它们的值,它使用对象的toString()方法。 所以,你可能有这样的代码: @Entity public class Genre { // fields and methods @Override public String toString() { return this.name; } } // in your GUI List genres = 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 ...
  • @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组件扩展了它们的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 ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。