标签: function-declaration

类方法的函数声明或表达式?

我和我的一个同事已经多次讨论过这个问题。定义类方法有两种方法。第一种方法是使用函数声明

class Action {
    public execute(): void {
        this.doSomething();
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)

函数声明往往更容易阅读。的每个实例仅使用一个函数对象Action,因此它们也更节省内存。

第二个是函数表达式

class Action {
    public execute: () => void = () => {
        this.doSomething();
    };
    ...
}
Run Code Online (Sandbox Code Playgroud)

函数表达式需要更多的输入(尤其是类型定义),更难阅读,并且会为 的每个实例生成一个新的函数对象Action。如果您生成大量对象,那就很糟糕了。

然而,函数表达式有一个小好处:无论谁调用它们,它们都会保留上下文this(即实例):Action

var instance = new Action();
setTimeout(instance.execute);
Run Code Online (Sandbox Code Playgroud)

在这种情况下,声明为函数表达式的方法按预期工作。函数声明会严重失败,但可以通过这样做轻松修复它们:

var instance = new Action();

setTimeout(() => instance.execute());
// or
setTimeout(instance.execute.bind(instance));
Run Code Online (Sandbox Code Playgroud)

那么,一种做法是否被认为比另一种更好,或者这纯粹是情况/偏好?

methods function-declaration typescript

2
推荐指数
1
解决办法
2802
查看次数

c++ - no matching function for call to

My terminal messages

zo@laptop:~/Desktop$ g++ stack.cpp 
stack.cpp: In function ‘int main(int, char**)’:
stack.cpp:50:19: error: no matching function for call to ‘boyInitial(Boy [boyNumber])’
     boyInitial(boy);
                   ^
stack.cpp:17:6: note: candidate: template<class T, int N> void boyInitial(T (&)[N])
 void boyInitial(T (&boy)[N]){
      ^~~~~~~~~~
stack.cpp:17:6: note:   template argument deduction/substitution failed:
stack.cpp:50:19: note:   variable-sized array type ‘long int’ is not a valid template argument
     boyInitial(boy);
                   ^
stack.cpp:51:19: error: no matching function for call to ‘getBoyAges(Boy [boyNumber])’
     getBoyAges(boy);
                   ^
stack.cpp:34:6: note: candidate: template<class T, int N> void getBoyAges(T …
Run Code Online (Sandbox Code Playgroud)

c++ templates function-declaration variable-length-array

2
推荐指数
1
解决办法
82
查看次数

具有 C11 标准的编译器接受的替代函数指针语法

我不确定这个函数指针类型声明语法,但它有效。语法就像声明一个普通的旧函数。

typedef struct Element GetChildCallback(void *userdata, usize parent, usize child_no);
Run Code Online (Sandbox Code Playgroud)

我找不到任何关于它是否符合标准或它可能有哪些缺点的信息。

我认为这仅适用于 typedef,所以我更进一步,发现这也适用于常规函数参数:

extern inline void dmpstrn(const char *t, usize n, int printer(const char *fmt, ...));

inline void dmpstrn(const char *t, usize n, int printer(const char *fmt, ...)) {
    usize len = strlen(t) > n ? n : strlen(t);
    printer("\"");
    for (usize i = 0; i < len; i += 1) {
        if (t[i] == '\n') 
            printer("\\n");
        else
            printer("%c", t[i]);
    }
    printer("\"\n");
}

// ...

int main() {
    dmpstrn("Hello\nworld", UINT64_MAX, …
Run Code Online (Sandbox Code Playgroud)

c typedef function-pointers implicit-conversion function-declaration

2
推荐指数
1
解决办法
76
查看次数

直接将数组写入参数会在 C++ 中出错

#include <iostream>

using namespace std;

template <typename VAR, unsigned int N>
int size(VAR (&arr)[N])
{
return sizeof(arr) / sizeof(arr[0]);
}

int main()
{
cout << size("Test"); //This is working

int x[] = {7, 5, 43, 8};
cout << endl
     << size(x); //And this works too

cout << endl
     << size({7, 9, 7, 9}); //When i try this its give me "no matching function for call to 'size'" error

return 0;
}
Run Code Online (Sandbox Code Playgroud)

参数采用在参数外部修改的字符串和数组。但我需要像上面的代码一样直接在函数内部写入数组。大小({一些整数});

c++ templates reference constants function-declaration

2
推荐指数
2
解决办法
97
查看次数

函数声明中的等式

在C++(Microsoft Visual Studio)中,我在mystring.h中有:main_savitch_4 :: string :: string(const char str [] ="")并添加到mystring.cpp中:

main_savitch_4::string::string(const char str[ ] = ""){
    allocated = 0;  
    current_length = 0;
    start = new char[0];
}, which defines the private variables. 
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

c:\users\erice\documents\c++_2\lab04\rice_lab04_2\rice_lab04_2\mystring.cpp(13): error C2572: 'main_savitch_4::string::string' : redefinition of default parameter : parameter 1
1>          c:\users\erice\documents\c++_2\lab04\rice_lab04_2\rice_lab04_2\mystring.h(88) : see declaration of 'main_savitch_4::string::string'
1>.  
Run Code Online (Sandbox Code Playgroud)

究竟出了什么问题?我不明白函数描述中的含义是什么

c++ function function-declaration

1
推荐指数
1
解决办法
268
查看次数

为什么我不能在JQuery的事件处理函数中将"函数声明"作为参数传递?

为什么这不能使用函数声明,但它使用函数表达式完美地工作?假设唯一的区别是浏览器如何将它们加载到执行上下文中.

function foo(event){
    console.log('in foo');
}

$('.btn').on('click',foo(event)); 

$.ajax({
    beforeSend:function(){
        $('btn').unbind('click');
    },
    success: function(){
        $('btn').bind('click', foo(event));
    }
});
Run Code Online (Sandbox Code Playgroud)

使用函数表达式它很有用:

var foo = function(event){
    console.log('in foo');
}
Run Code Online (Sandbox Code Playgroud)

javascript jquery event-handling function-declaration function-expression

1
推荐指数
1
解决办法
432
查看次数

C函数typedef:定义没有参数列表的函数

我的程序有几十个(可能超过100个)具有相同参数列表和返回类型的函数.我也可能想为这些函数添加参数.那么,有没有办法用typedef原型(带参数列表)定义这些函数?

例如:我有几十个函数,int f1 (int, int)所以我有几十个声明,如:

int f1 (int x, int y){...}
int f2 (int x, int y){...}
....
int fn(int x, int y){...}
Run Code Online (Sandbox Code Playgroud)

我想定义一些类似于:

typedef int functiontype(int x, int y);
functiontype f1{...}
...
functiontype fn{...}
Run Code Online (Sandbox Code Playgroud)

因此,当我需要升级这些函数时(例如,使用新参数z)我只需要升级typedef语句.这有可能吗?

c typedef function-declaration

1
推荐指数
1
解决办法
151
查看次数

'main'函数是否被归类为C中的函数定义?

'main'函数是否被归类为C中的函数定义?

我问的原因是我被提供了一段代码,在解释代码顶部的函数声明和底部的函数定义之间的区别时,我被问到是否也考虑了'main'函数函数定义或者如果它被认为是其他东西(因为主要函数与其他函数不同).

c function-declaration

1
推荐指数
1
解决办法
216
查看次数

为什么即使在调用之前未定义函数调用,函数调用仍然有效?

我做过这个测验,并不理解输出

#include <stdio.h>
int main()
{
    void demo();
    void (*fun)();
    fun = demo;
    (*fun)();
    fun();
    return 0;
}

void demo()
{
    printf("GeeksQuiz ");
}
Run Code Online (Sandbox Code Playgroud)

预期:编译错误,因为我认为通常demo()需要在调用之前初始化main()

实际结果: GeeksQuiz GeeksQuiz

我的假设是错误的,通常需要在调用它们之前定义函数吗?

c forward-declaration function-declaration

1
推荐指数
1
解决办法
41
查看次数

How to tell if a function is built-in or self-defined by its name?

I generate a call graph of a complex MATLAB system, and I want to know which functions are built-in and mark them.

matlab function built-in call-graph function-declaration

1
推荐指数
1
解决办法
60
查看次数