Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;作为参数的新对象(如Java) 我在C++中编码了一些东西,而且我们总是有一些“想法”,如果语言上有类似的东西……_Java_C++_Class_Object_Coding Style - Fatal编程技术网

C++;作为参数的新对象(如Java) 我在C++中编码了一些东西,而且我们总是有一些“想法”,如果语言上有类似的东西……

C++;作为参数的新对象(如Java) 我在C++中编码了一些东西,而且我们总是有一些“想法”,如果语言上有类似的东西……,java,c++,class,object,coding-style,Java,C++,Class,Object,Coding Style,我现在对作为参数传递的对象表示怀疑 我经常做的是创建一个新对象,并将其实例作为指针传递,如下所示: mObject* obj1 = new mObject(); obj1->callback(the_callback_function); function_that_needs_obj(obj1); // (or the reference, if I didn't instantiated a pointer). function_that_needs_obj(new mObject

我现在对作为参数传递的对象表示怀疑

我经常做的是创建一个新对象,并将其实例作为指针传递,如下所示:

mObject* obj1 = new mObject();
obj1->callback(the_callback_function);

function_that_needs_obj(obj1); // (or the reference, if I didn't instantiated a pointer).
function_that_needs_obj(new mObject(){
    public void onCallback(){
        // Custom code enters here....
    }
});
function_that_needs_obj([]()
                        {
                           // Custom code enters here....
                        });
没关系,工作正常,这没问题。但是,当我使用Java时,我曾经这样做:

mObject* obj1 = new mObject();
obj1->callback(the_callback_function);

function_that_needs_obj(obj1); // (or the reference, if I didn't instantiated a pointer).
function_that_needs_obj(new mObject(){
    public void onCallback(){
        // Custom code enters here....
    }
});
function_that_needs_obj([]()
                        {
                           // Custom code enters here....
                        });
<>这有没有C++中的“排序”?因为对于我需要创建的每个回调:一个
扩展类
,或者一个
functionnamedverylong,因为etherearemany
必须写在代码之上,这种“混淆”代码很多


而且,如果我调用函数回调,则无法通过
访问所有对象变量…

如果编译器支持c++11,则可以使用如下lambda表达式:

mObject* obj1 = new mObject();
obj1->callback(the_callback_function);

function_that_needs_obj(obj1); // (or the reference, if I didn't instantiated a pointer).
function_that_needs_obj(new mObject(){
    public void onCallback(){
        // Custom code enters here....
    }
});
function_that_needs_obj([]()
                        {
                           // Custom code enters here....
                        });

如果您的编译器支持c++11,您可以像这样使用lambda表达式:

mObject* obj1 = new mObject();
obj1->callback(the_callback_function);

function_that_needs_obj(obj1); // (or the reference, if I didn't instantiated a pointer).
function_that_needs_obj(new mObject(){
    public void onCallback(){
        // Custom code enters here....
    }
});
function_that_needs_obj([]()
                        {
                           // Custom code enters here....
                        });

你可以在boost lambda中找到你需要的东西

你可以在boost lambda中找到你需要的东西访问
对象数据,除非它使用全局作用域。抱歉。。。我不是英国人……C++中的回调更容易。只需使用任何可调用类型并传递lambda。@chris,“实现”回调没有问题,只是增强代码而已。。。如果我向新对象发送可调用类型,可调用类型将无法
访问
对象数据,除非它使用全局作用域。抱歉。。。我不是英国人…对不起,我从来没有听说过编译器“Arduino”实际上不是一个编译器,它使用GCC,但有什么方法可以“启用”这个吗?对不起,我从来没有听说过编译器“Arduino”实际上不是一个编译器,它使用GCC,但有什么方法可以“启用”这个吗?