簡體   English   中英

調用不同類型的函數

[英]Calling a function with different types

我正在用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:錯誤:對於函數樣式強制轉換或類型構造,應為'('

我無法使char []類型正常工作,以上語法是否正確?

試試這個片段:

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 []

請注意,在上面的示例中,參數正在更改,而不是返回值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM