小编Han*_*123的帖子

功能指针 - 2个选项

我想知道这两个函数之间有什么区别(funfun2)我知道这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)

c++ function-pointers

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

类 - 具有动态大小的用户定义的智能阵列

我正在编写以下数组(类),当此数组的索引大于此数组的大小时,该数组会增加大小.我知道矢量但它必须是数组.该代码如下所示:

#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)

c++ arrays memory-management class operator-overloading

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