C++的int初始化
生活随笔
收集整理的這篇文章主要介紹了
C++的int初始化
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
int MyInt(56);
std::cout<<"MyInt="<<MyInt<<std::endl;
int MySecondInt = int();????????//int變量可以這樣初始化 std::cout<<MySecondInt<<std::endl;
//那么可不可以這樣初始化呢?? int MyThirdInt(int());? ??//error!!! now, this is exactly a function ????????
所以?MyThirdInt?正確的寫法是
int MyThirdInt(int()) { ? ? ? ? // other code... ? return 0; }
既然MyThirdInt?是一個函數, 那么里面的參數是什么東東呢?
//我們在聲明一個函數 int OtherInt() { return 33; }
//再看三行代碼 int (*pfunc)(); pfunc = OtherInt; std::cout<<"MyThirdInt is: "<<MyThirdInt(pfunc)<<std::endl;
MyThirdInt?的參數是個函數指針,但是函數指針好像不能取出來....
所以這個特殊的Int變量需要重新定義,就像下面這樣:
int MyThirdInt(int (*pInt)()) { return pInt();
}
======================完整代碼========================
// hellomsg.cpp :? //
#include "stdafx.h" #include <iostream>
int OtherInt() { return 33; }
int MyThirdInt(int (*pInt)()) {
return pInt(); }
int _tmain(int argc, _TCHAR* argv[]) { int MyInt(56); std::cout<<"MyInt="<<MyInt<<std::endl;
int MySecondInt = int(); std::cout<<MySecondInt<<std::endl;
int (*pfunc)(); pfunc = OtherInt; std::cout<<"MyThirdInt is: "<<MyThirdInt(pfunc)<<std::endl;
return 0; }
int MySecondInt = int();????????//int變量可以這樣初始化 std::cout<<MySecondInt<<std::endl;
//那么可不可以這樣初始化呢?? int MyThirdInt(int());? ??//error!!! now, this is exactly a function ????????
所以?MyThirdInt?正確的寫法是
int MyThirdInt(int()) { ? ? ? ? // other code... ? return 0; }
既然MyThirdInt?是一個函數, 那么里面的參數是什么東東呢?
//我們在聲明一個函數 int OtherInt() { return 33; }
//再看三行代碼 int (*pfunc)(); pfunc = OtherInt; std::cout<<"MyThirdInt is: "<<MyThirdInt(pfunc)<<std::endl;
MyThirdInt?的參數是個函數指針,但是函數指針好像不能取出來....
所以這個特殊的Int變量需要重新定義,就像下面這樣:
int MyThirdInt(int (*pInt)()) { return pInt();
}
======================完整代碼========================
// hellomsg.cpp :? //
#include "stdafx.h" #include <iostream>
int OtherInt() { return 33; }
int MyThirdInt(int (*pInt)()) {
return pInt(); }
int _tmain(int argc, _TCHAR* argv[]) { int MyInt(56); std::cout<<"MyInt="<<MyInt<<std::endl;
int MySecondInt = int(); std::cout<<MySecondInt<<std::endl;
int (*pfunc)(); pfunc = OtherInt; std::cout<<"MyThirdInt is: "<<MyThirdInt(pfunc)<<std::endl;
return 0; }
總結
以上是生活随笔為你收集整理的C++的int初始化的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ChatGPT来袭,美大学开始改变教学方
- 下一篇: 今年是农历兔年,也是双春年,意味着有两个