我想知道这两个函数之间有什么区别(fun和fun2)我知道这fun2是函数指针但是有什么用fun?这是相同的,因为还有通过指针的功能名称吗?
#include <iostream>
void print()
{
std::cout << "print()" << std::endl;
}
void fun(void cast())
{
cast();
}
void fun2(void(*cast)())
{
cast();
}
int main(){
fun(print);
fun2(print);
}
Run Code Online (Sandbox Code Playgroud) 我正在编写以下数组(类),当此数组的索引大于此数组的大小时,该数组会增加大小.我知道矢量但它必须是数组.该代码如下所示:
#include <iostream>
using namespace std;
class Array {
public:
Array():_array(new float[0]), _size(0){};
~Array() {delete[] _array;}
friend ostream &operator<<(ostream&,const Array&);
float& operator[] (int index)
{
if(index>=_size)
{
float* NewArray=new float[index+1];
for(int i=0;i<_size;++i) NewArray[i]=_array[i];
for(int i=_size;i<index+1;++i) NewArray[i]=0;
delete[] _array;
_array=NewArray;
_size=index+1;
}
return _array[index];
}
private:
float *_array; // pointer to array
int _size; // current size of array
};
ostream &operator << ( ostream &out, const Array& obj) // overloading operator<< to easily print array
{
cout << …Run Code Online (Sandbox Code Playgroud)