调用具有不同类型的函数

时间:2015-09-02 23:24:24

标签: c++ c++11

我正在使用C ++进行作业,并且遇到问题困难。 我们给出了一个可以作为参数接受任何内容的函数。我们需要将此函数UberFunction称为int,int *,int **和char []。

我的代码:

void CallUberFunctions() {
  // There is a magic function, called "UberFunction" that is capable of taking
  // anything for an argument. You need to call "UberFunction" with the
  // following types: int, int* int** and char[]
  // You also need to call "OtherUberFunction" in the namespace
  // "uber_namespace" once, with no arguments.
  UberFunction(42);
  UberFunction((int*)42);
  UberFunction((int**)42);
  UberFunction((char[])'*');
}

以下是错误:

  

home / user / Desktop / cpp_refresher / cpp_refresher.cc:22:20:error:expected'(' for function-style         铸造或类型结构

我无法让char []类型正常工作,我的语法不正确吗?

1 个答案:

答案 0 :(得分:0)

试试这个片段:

const int value = 42;
UberFunction(value); // int
UberFunction(&value); // int *
int * p_value = &value;
UberFunction(&p_value); // int * *
static const char text[] = "Hello World!\n";
UberFunction(text); // char []

请注意,在上面的示例中,参数正在更改,而不是返回值。

相关问题