简体   繁体   English

C ++函数参数或参数数组

[英]C++ function parameters or parameter array

My background and instincts tell me I should always create a tighter and more explicit interface to a function by requiring parameters like this: 我的背景和本能告诉我,应该始终通过要求如下参数来为函数创建更紧密,更明确的接口:

bool someFunc(int param1, int param2, char param3, float param4) {
   ...
}

or requiring an object (struct or class) like: 或需要以下对象(结构或类):

class someObject {
    ...
    int p1;
    int p2;
    char c1;
    float p4;
}

I have been told by my boss that I should be using something like: 我的老板告诉我,我应该使用类似的东西:

bool someFunc(void *params[], int size) {
   ...
}

because it creates more extensible (you can iterate over parameters this way) and faster code. 因为它创建了更可扩展的代码(您可以通过这种方式遍历参数)和更快的代码。

I am only interested in improving my abilities, but my instincts go against this method. 我只对提高自己的能力感兴趣,但是我的直觉与此方法背道而驰。 Is he right? 是吗

Horrible idea. 可怕的主意。 I can't list the reasons why it's bad in a single answer, but the main problem is that it just doesn't work. 我无法在一个答案中列出它不好的原因,但是主要的问题是它根本不起作用。 As a simple example, you can't pass 3, and if you pass 0 it becomes a nullptr. 举一个简单的例子,您不能传递3,如果传递0,它将成为nullptr。 More importantly, you have to cast the values back to a given type anyway, so why not specify the type in the signature? 更重要的是,无论如何您都必须将值转换回给定的类型,那么为什么不在签名中指定类型呢?

Now there's a real C++ alternative, variadic templates: 现在有一个真正的C ++替代方法,可变参数模板:

template<typename... Arguments>
void Foo(Arguments... parameters);

In this case, the compiler will know all the types in Arguments... , there's no need to cast anything. 在这种情况下,编译器将知道Arguments...所有类型,而无需强制转换任何内容。

I disagree with the above answer, your instincts are right you should try and create a explicit interface to a function. 我不同意上面的答案,您的直觉是正确的,您应该尝试为函数创建一个显式接口。 There are times where you can't have an explicit interface, and then you should consider variadic template arguments, but they're rare. 有时您可能没有显式接口,然后应考虑可变参数模板参数,但这种情况很少见。 Two Ints a char and float seem like a perfectly reasonable function parameters. 一个char和float两个整数似乎是一个非常合理的函数参数。

However you're in a very sticky situation, you don't want to go against your boss. 但是,您处在非常棘手的情况下,不想与您的老板对立。 I would be skeptical of any programming advice from him, he's either not a very good programmer or worse one of those old school hacky c programmers (see if he uses MACROS everywhere). 我会对他提出的任何编程建议表示怀疑,他不是一个很好的程序员,或更糟糕的是那些那些老派的黑C程序员(看看他是否在各处都使用了MACROS)。 My advice is to do it his way now, then if you're ever working with that function again later fix it and try and get someone else to review your code. 我的建议是按现在的方式进行操作,然后,如果您再次使用该功能,请稍后对其进行修复,并尝试让他人检查您的代码。

Your boss is crazy. 你的老板疯了。 Such debauchery had some place in C, and even there you would use varargs , not this crazy construct. 这样的放荡在C中占有一席之地,即使在那儿,您也将使用varargs ,而不是这种疯狂的构造。 If you want to program in a dynamically-typed language, either don't use C++, or use arrays of variant types (say boost::variant or QVariant ). 如果要使用动态类型的语言进行编程,请不要使用C ++或使用变量类型的数组(例如boost::variantQVariant )。

The "extensibility" your boss is looking for and obviously missing is otherwise known as function/method overloading. 您的老板正在寻找的“可扩展性”而显然缺少的“可扩展性”也称为函数/方法重载。 It has its place as a part of a proper design, not as an edict. 它的位置是适当设计的一部分,而不是法令。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM