首页 \ 问答 \ 不同的构造方式(The different constructor ways)

不同的构造方式(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
更新时间:2023-08-09 14:08

最满意答案

您总是可以使用自己的绑定来包装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

相关问答

更多

最新问答

更多
  • 获取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的基本操作命令。。。