English 中文(简体)
如何援引C++的功能?
原标题:How to invoke C++ function?
  • 时间:2011-07-04 07:02:34
  •  标签:
  • c++
  • function

我具有原型的功能。

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

我知道,我们可以将原封成: 避免试验(int*, int);

在<代码>main()中,我们宣布以下阵列:

int array1[10], array2[10];

将职能机构设至0,

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

}

能够、为什么?

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. I am just wondering if this is possible and is it a good choise?

最佳回答

您可能会询问正式参数和实际参数之间的区别。

In your prototype

void test(int *array, size_t size);

姓名和面积是正式参数。 你在你的职责范围内使用这些名字。

In the code that invokes the function, you can use different names, which are the actual parameters.

......

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

这里的array1 b是第一次援引的实际参数,array2b是第二次援引的实际参数。

So yes, you can use whatever names you like as actual parameters, ...... long as the types of the variables match your prototype.

问题回答

不是直接回答你的问题,但你谈到C++,使用旧的C阵列,这引起了我的注意:

Consider not using C arrays in the first place. Instead, use a 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.

您的职能签署将仅仅是

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

采用零填充病媒的方法是:

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

是的,这是可能的;但职能机构内的申报应当与你宣布的原型相同:

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

http://www.cplus.com/vis/clibrary/cstring/memset/“rel=“nofollow”>memset()。 如果您想制定<代码>0

(作为建议,你可以建立一个以现有内容为基础的图书馆。) 否则,它就好像重新发明轮椅。

从C. 是的角度来看,这是可能的,但你们需要获得声明的权利,或者汇编者会犯错误。

理想的C++原型将是

void test(int *array, size_t size);

在C++中,你必须申报回归类型以及原型和执行中每一论点的类型。

Note: You don t need to use size_t, but it is preferred (even on C). size_t is included in stddef.h (and by extension cstddef which is the preferred C++ include). It is architecture dependent and is usually unsigned int in 32-bit systems and unsigned long long on 64-bit systems





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?