简体   繁体   English

在函数调用中创建的对象与传入的对象之间有什么区别

[英]What's the difference between an object created in function call and object passed in

This is obviously a hole in my self taught computer science education... 这显然是我自学计算机科学教育的一个漏洞。

The constructor for a text control (wxTextCtrl) in a wxWidgets application has an optional parameter for a validator object. wxWidgets应用程序中的文本控件(wxTextCtrl)的构造函数具有一个用于验证器对象的可选参数。 All of the code examples create the validator on the fly within the constructor for the text control. 所有代码示例都在文本控件的构造函数中动态创建验证器。

This works.. 这有效..

wxString value = L"0.0";
wxTextCtrl* _Text = new wxTextCtrl(this, wxID_ANY, value, 
    wxDefaultPosition, wxDefaultSize, 0, 
    wxTextValidator(wxFILTER_NUMERIC, &value));

However in my particular case I want to create the validator in another function and pass it back, which isn't working. 但是,在我的特定情况下,我想在另一个函数中创建验证器并将其传递回去,这是行不通的。 As an intermediate step I've tried to create it just before creating the wxTextCtrl and pass it in but that doesn't work either... 作为一个中间步骤,我尝试在创建wxTextCtrl并将其传递之前创建它,但这也不起作用...

wxString value = L"0.0";
wxValidator valid = wxTextValidator(wxFILTER_NUMERIC, &value);
wxTextCtrl* _Text = new wxTextCtrl(this, wxID_ANY, value, 
    wxDefaultPosition, wxDefaultSize, 0, valid);

Although this compiles and runs it doesn't perform the validation. 尽管这会编译并运行,但不会执行验证。 Can anyone explain why? 谁能解释为什么?

The prototype for the wxTextValidator calls for a constant reference.. wxTextValidator的原型需要一个常量引用。

wxTextCtrl::wxTextCtrl  (   wxWindow *  parent,
    wxWindowID  id,
    const wxString &    value = wxEmptyString,
    const wxPoint &     pos = wxDefaultPosition,
    const wxSize &  size = wxDefaultSize,
    long    style = 0,
    const wxValidator &     validator = wxDefaultValidator,
    const wxString &    name = wxTextCtrlNameStr 
)

You have sliced the wxTextValidator object when you assigned it to a variable of type wxValidator , its base class. wxTextValidator对象分配给其基类wxValidator类型的变量时,已对它进行了切片 To fix this, you'll need to preserve the more specific type: 要解决此问题,您需要保留更具体的类型:

wxTextValidator valid = wxTextValidator(wxFILTER_NUMERIC, &value);

You can use auto to avoid repeating yourself. 您可以使用auto来避免重复自己。

Alternatively, you can use lifetime extension which occurs when assigning a temporary to a const reference: 另外,您可以使用生命周期扩展 ,这是在将临时const分配给const引用时发生的:

const wxValidator& valid = wxTextValidator(wxFILTER_NUMERIC, &value);

This works because there is no copy, and therefore no slicing. 之所以有效,是因为没有副本,因此也没有切片。

Note that when designing your own classes, it's often a good idea to prevent object slicing by making your base classes abstract, or making their (copy) constructors protected . 请注意,在设计自己的类时,通过使基类抽象化或使它们的(副本)构造函数protected来防止对象切片通常是一个好主意。

暂无
暂无

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

相关问题 Object obj = Object()和Object obj()有什么区别? - What's difference between Object obj = Object() and Object obj()? 普通object和object临时传递的普通参数、引用参数和const引用参数的区别 - Difference between ordinary parameter, reference parameter and const reference parameter passed by ordinary object and object created temporary (* object)[0]和(* object)[1]有什么区别 - what is the difference between (*object)[0] and (*object)[1] 在c ++中派生对象和基础对象之间有什么区别? - What's the difference between a derived object and a base object in c++? 在 C++ 中,对象和指向对象的指针有什么区别? - in C++, what's the difference between an object and a pointer to an object? `Object obj(args ...)`和`Object obj {args ...}`有什么区别? - What's the difference between `Object obj(args…)` and `Object obj{args…}`? 模板化模式对象和外部密码对象有什么区别? - What's the difference between templated mode object and external cipher object? 在C ++中使用和不使用构造函数调用初始化对象之间的区别是什么 - What is the difference between initialising an object with and without a constructor call in C++ 标准C ++ 11是否保证在函数调用之前创建传递给函数的临时对象? - Does standard C++11 guarantee that temporary object passed to a function will have been created before function call? 这两种对象实例化方法有什么区别? - What's the difference between these two object instantiation approaches?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM