简体   繁体   English

如何调用 C++ function?

[英]How to invoke C++ function?

I have a function with the prototype as我有一个原型为 function

void test( int array [] , int b); 

I know we can replce the protoype as: void test(int*, int);我知道我们可以将原型替换为: void test(int*, int);

In main() we declare the following arrays:main()中,我们声明了以下 arrays:

int array1[10], array2[10];

To set the body of the function to 0,要将 function 的主体设置为 0,

test ( array1 , b)
{
for ( int i = 0 ; i < b ; i++)
  array1[i] = 0;

}

can i do the follwing and why?我可以这样做吗?为什么?

int main()
{// assuming b is the size of the array
test(array1 , b);
test(array2 , b) ;
return 0;
}

i know the basic of c++ im trying to write my own include files.我知道 c++ 的基本知识,我正在尝试编写自己的包含文件。 I am just wondering if this is possible and is it a good choise?我只是想知道这是否可能,这是一个不错的选择吗?

Not a direct answer to your question, but the fact that you talk about C++ and yet use plain old C arrays caught my attention:不是对您的问题的直接回答,但是您谈论 C++ 却使用普通的旧 C arrays 的事实引起了我的注意:

Consider not using C arrays in the first place.首先考虑不使用 C arrays。 Instead, use a std::vector<int> .相反,使用std::vector<int> This probably avoids the need to ask this question in the first place (and it avoids a whole lot of other issues).这可能避免了首先提出这个问题的需要(并且它避免了很多其他问题)。 You don't need to bother about the right size type ( int ? size_t ? Something else?) since std::vector gives you the right type already: std::vector<int>::size_type .您无需担心正确的尺寸类型( intsize_t ?其他?),因为std::vector已经为您提供了正确的类型: std::vector<int>::size_type

Your function signature would just be您的 function 签名将是

void test( std::vector<int> &a );

The implementation for filling the vector with zeros would be:用零填充向量的实现将是:

void test( std::vector<int> &a )
{
  std::fill( a.begin(), a.end(), 0 );
}

Yes, it's possible;是的,这是可能的; but declaration in the function body should be same as what you declared as prototype:但 function 正文中的声明应与您声明为原型的内容相同:

void test (int array1[], int b) // <---- see here (prefer `unsigned int` for size)
{
  for ( int i = 0 ; i < b ; i++)
    array1[i] = 0;
}

It's better to use library function memset() if you want to set something to 0 .如果要将某些内容设置为0 ,最好使用库 function memset()

(As an advise, you can a build a library on top of what is already existing. Otherwise it will be like reinventing a wheel.) (作为建议,您可以在已经存在的基础上构建一个库。否则就像重新发明轮子一样。)

You may be asking about the difference between formal parameters and actual parameters.您可能会问形式参数和实际参数之间的区别。

In your prototype在您的原型中

void test(int *array, size_t size);

the names 'array' and 'size' are the formal parameters.名称“array”和“size”是形式参数。 You use those names inside the body of your function.您在 function 的正文中使用这些名称。

In the code that invokes the function, you can use different names, which are the actual parameters.在调用function的代码中,可以使用不同的名称,即实际参数。

so所以

int main()
{
   const size_t b = 10;
   int array1[10], array2[10];
   test(array1 , b);
   test(array2 , b) ;
   return 0;
}

Here array1 and b are the actual parameters to the first invocation and array2 and b are the actual parameters to the second invocation.这里array1b是第一次调用的实际参数, array2b是第二次调用的实际参数。

So yes, you can use whatever names you like as actual parameters, so long as the types of the variables match your prototype.所以是的,您可以使用任何您喜欢的名称作为实际参数,只要变量的类型与您的原型匹配。

Looks like you're migrating from C.看起来您正在从 C 迁移。 Yes, this is possible, but you need to get the declarations right, or the compiler will throw an error.是的,这是可能的,但您需要正确声明,否则编译器会抛出错误。

The preferred C++ prototype would be首选的 C++ 原型是

void test(int *array, size_t size);

In C++, you must declare the return type, and the type(s) of each argument in both the prototype and the implementation.在 C++ 中,您必须声明返回类型,以及原型和实现中每个参数的类型。

Note: You don't need to use size_t , but it is preferred (even on C).注意:您不需要使用size_t ,但它是首选(即使在 C 上)。 size_t is included in stddef.h (and by extension cstddef which is the preferred C++ include). size_t包含在stddef.h中(扩展名为cstddef ,这是首选的 C++ 包含)。 It is architecture dependent and is usually unsigned int in 32-bit systems and unsigned long long on 64-bit systems它依赖于体系结构,通常在 32 位系统中为 unsigned int,在 64 位系统中为 unsigned long long

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

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