不同的构造方式(The different constructor ways)
我写了一些小班,现在我看到了不同的方法来调用构造函数。 哪种方式正确? (或者更好的方式)
SharedKeys.h
#ifndef SHAREDKEYS_H #define SHAREDKEYS_H class SharedKeys { private: char keyCode; public: SharedKeys(char keycode) { keyCode = keycode; } ~SharedKeys() {} char getKeyCode() { return keyCode; }; }; #endif
Main.cpp的
我的构造函数调用方式:
SharedKeys* SK; SK = new SharedKeys(*cstr); //Call contructor
其他构造函数调用方式:
// SharedKeys constructor SharedKeys::SharedKeys(*cstr) { keyCode = keycode; }
我愿意接受任何改进建议。 谢谢。
Main.cpp的
SK必须是这里的指针还是? (此代码有效)
SharedKeys* SK; vector<SharedKeys> StoreSharedKeys; string str = x; char *cstr = new char[str.length() + 1]; strcpy(cstr, str.c_str()); SK = new SharedKeys(*cstr); StoreSharedKeys.push_back(*SK); delete[] cstr;
我理解正确吗?
cout << "Key: " << x << " loaded." << endl; string str = x; char *cstr = new char[str.length() + 1]; strcpy(cstr, str.c_str()); SharedKeys SK(*cstr); StoreSharedKeys.push_back(SK); delete[] cstr;
如果我看到正确,那么这个构造函数调用是正确的方法。 所以我不必删除对象。 正确?
I have write little class, now i see different ways to call constructors. Which way is the correct way? (Or the better way)
SharedKeys.h
#ifndef SHAREDKEYS_H #define SHAREDKEYS_H class SharedKeys { private: char keyCode; public: SharedKeys(char keycode) { keyCode = keycode; } ~SharedKeys() {} char getKeyCode() { return keyCode; }; }; #endif
Main.cpp
My constructor call way:
SharedKeys* SK; SK = new SharedKeys(*cstr); //Call contructor
Other constructor call way:
// SharedKeys constructor SharedKeys::SharedKeys(*cstr) { keyCode = keycode; }
I am open to any suggestions for improvement. Thank you.
Main.cpp
SK must be a pointer here or? (this code works)
SharedKeys* SK; vector<SharedKeys> StoreSharedKeys; string str = x; char *cstr = new char[str.length() + 1]; strcpy(cstr, str.c_str()); SK = new SharedKeys(*cstr); StoreSharedKeys.push_back(*SK); delete[] cstr;
Did I understand you correctly?
cout << "Key: " << x << " loaded." << endl; string str = x; char *cstr = new char[str.length() + 1]; strcpy(cstr, str.c_str()); SharedKeys SK(*cstr); StoreSharedKeys.push_back(SK); delete[] cstr;
if im see that correct then this contructor call is the right way. So i dont must delete the objects. Correct?
原文:https://stackoverflow.com/questions/44923410
最满意答案
您总是可以使用自己的绑定来包装with绑定,该绑定使用jQuery作为某些动画,如下所示:
ko.bindingHandlers['fadingWith'] = { init: function(element, valueAccessor, allBindingsAccessor, context) { return ko.bindingHandlers['with']['init'](element, valueAccessor, allBindingsAccessor, context); }, update: function(element, valueAccessor, allBindingsAccessor, context) { $(element).fadeOut(100, function () { ko.bindingHandlers['with']['update'](element, valueAccessor, allBindingsAccessor, context) }).fadeIn(100); } }; ko.virtualElements.allowedBindings['fadingWith'] = true;
然后你可以像这样应用它:
<div data-bind="fadingWith: someObservable"><span data-bind="text: $data"></span></div>
我没有测试过这个(我稍后会试一试),但我认为这将是我要走的路。
另一个选项(我更确定会工作)是您创建一个单独的绑定,执行以下操作:
ko.bindingHandlers['fadeOn'] = { update: function(element) { $(element).hide().fadeIn(200); } }
这并没有为您提供可观察更改之前的动画,但它会在之后为您提供动画。 所以你要做
<div data-bind="with: someObservable, fadeOn: someObservable"><span data-bind="text: $data"></span></div>
编辑:我刚才提出的另一个可能更容易的选择是使用你正在使用的变量的节流扩展:
视图模型:
///...your code.... this.observableThatNeedsWith = ko.observable("Hello"); this.delayedObservable = ko.computed(this.observableThatNeedsWith).extend({throttle: 200}); //...continue your code
然后,您有一个绑定如下:
ko.bindingHandlers['fadeInOut'] = { update: function(element) { $(element).stop(true, true).stop(true, true).fadeOut(200).fadeIn(200); } }
请注意,淡出时间与节流时间相同。
然后你像这样绑定它:
<div data-bind="with: delayedObservable, fadeInOut: observableThatNeedsWith"><span data-bind="text: $data"></span></div>
将要发生的是当您更改observableThatNeedsWith时,fadeInOut处理程序将开始转换元素。 然后在它完成渐渐消失的时刻(在这种情况下为200ms),油门将赶上并且delayedObservable将在fadeInOut开始逐渐淡化元素时更新。它淡出一件事,然后淡入另一件事。
I'm resolved problem by creating binding that create copy of the element. After clone this element I animate it and after that I'm delete it and animate orginal element with new values. It is important to set this binding before with
相关问答
更多-
为什么绑定更新没有实现INotifyPropertyChanged?(Why does the binding update without implementing INotifyPropertyChanged?)[2023-07-03]
我测试了它,你是对的。 现在我在网上搜索它,并找到了它 。 对不起,花了这么长的时间来回复,实际上你遇到了WPF的另一个隐藏方面 ,那就是WPF的数据绑定引擎将数据绑定到PropertyDescriptor实例 ,该实例包装源属性,如果源对象是纯CLR对象并且不实现INotifyPropertyChanged接口。 数据绑定引擎将尝试通过PropertyDescriptor.AddValueChanged()方法来订阅属性已更改的事件。 当目标数据绑定元素更改属性值时,数据绑定引擎将调用PropertyDe ... -
Combobox绑定不会更新(Combobox binding does not update)[2023-09-06]
最后我得到了答案。 我使用了错误的属性。 我必须绑定“SelectedValue”属性而不是“SelectedItem”属性。 Finally I got the answer. I was using the wrong porperty. I must bind "SelectedValue" property instead "SelectedItem" property. -
动态绑定更新映像(Dynamic binding update image)[2023-05-31]
您需要在项类中实现INotifyPropertyChanged : public class MyDataItem : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private ImageSource image; public ImageSource Image { get { return image; } set ... -
使用INotifyPropertyChanged是DependencyProperties的一个很好的替代方法。 如果实现接口,则可以使用null作为参数引发PropertyChanged事件,以通知UI所有属性都已更改。 Using INotifyPropertyChanged is a good alternative to DependencyProperties. If you implement the interface you can raise the PropertyChanged eve ...
-
ObservableCollection的任务是向听众宣布更改(添加,删除......)。 所以ObservableCollection没有任何问题,并将它绑定为ListBox的ItemsSource ,不会使它无法编辑。 GetIcon方法一定有问题(例如,尝试重新打开未正确关闭的相同图像) 要查看这个想法是否正确,请尝试以下方法: ObservableCollection
filesList = new ObservableCollection (); filesList. ... -
我不会在构造函数中传递它,但我会使用另一种形式的公共属性。 之后,如果所有绑定都正确完成,您可以在第一个表单的网格中调用refresh,但也可以立即看到更改。 显示你的绑定。 I would not pass it in the constructor but i would use a public property of the other form. after that if all your bindings are done correctly you can call refresh in t ...
-
您总是可以使用自己的绑定来包装with绑定,该绑定使用jQuery作为某些动画,如下所示: ko.bindingHandlers['fadingWith'] = { init: function(element, valueAccessor, allBindingsAccessor, context) { return ko.bindingHandlers['with']['init'](element, valueAccessor, allBindingsAccessor, con ...
-
绑定不会更新(Binding wont update)[2023-08-21]
哦,我找到了有趣的事实。 绑定在反序列化后才起作用。 当我创建新的结构等..它的工作原理但是当我使用XMLSerializer将这个结构序列化为XML然后反序列化这个类中的每个绑定时都不起作用,所以我可以更改所有值但是它没有在GUI中更新...很奇怪 Thank you for your request, it is really useful, but I´ve found where the problem is. I have two collections in my application. I ... -
我能够找到一个关于如何动态更新动画版的答案。 start是一个回调,它在动画开始时被触发,但对props对象的修改没有反映在动画中! 为此, step回调是理想的,它让我们在每个动画步骤中更改fx对象。 以下是我想出的解决方案。 el.delay(delay).animate({ top: -distance + 'px' },{ duration:700, step:function(now,fx){ if(now == 0){ f ...
-
使用{Binding。}或{Binding}时数据绑定不更新(Databinding Not Updating When Using {Binding .} or {Binding})[2023-11-11]
绑定到Address对象本身时,对象本身(即其标识)不会更改,即使其属性也是如此。 因此,WPF不知道在这种情况下刷新绑定。 所以,是的,您需要绑定到通知属性(或属性)而不是整个对象。 正如您所说,一种方法是创建一个DisplayText属性,并在影响显示文本的内容发生更改时为该属性引发PropertyChanged事件。 另一种是在水平定向的StackPanel中使用多个TextBlock,每个TextBlock都绑定到特定的属性,例如相关文章
更多- [ solr扩展 ] Different ways to implement autosuggest using SOLR
- 关于如何参与到开源项目中《How To Succeed In Open Source ( In Ways You Haven't Considered Yet )》
- Hadoop 基本构造块
- 关于子类构造器中调用父类构造器的问题
- 解决get方式中文乱码的问题
- 在html使用CSS的方式
- 思考方式--SMART原则
- Hibernate 注释方式示例
- RabbitMQ 消息确认方式
- Hadoop 压缩文件方式比较
最新问答
更多- 获取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的基本操作命令。。。