-
-
Save hmito/ae0920588df8081b6f44 to your computer and use it in GitHub Desktop.
それ、ポインタ使わなくてもできるよ:C言語のポインタとC++の流儀 ref: https://qiita.com/hmito/items/44925fca9fca74e78f61
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
std::function<double(double,double)> Func = [](double val1, double val2)->double{return std::max(val1*val1,val2*val2);}; | |
double Ans = Func(10,-12); | |
//Ans==144 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void dog(){printf("bowwow");} | |
void cat(){printf("meow");} | |
int main(void){ | |
//ここで、戻り値void、引数voidの関数ポインタを用意 | |
void (*CryFuncPtr)(void); | |
if(LikeDog){ | |
CryFuncPtr = dog; | |
}else{ | |
CryFuncPtr = cat; | |
} | |
printf("My pet is crying!"); | |
//これで関数を呼び出せる | |
CryFuncPtr(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct data{ | |
int a; | |
int b; | |
}; | |
int main(){ | |
data A; | |
data* Ptr=&A; //ポインタは、Type*で宣言し、アドレスを受け取る。 | |
data& Ref=A; //参照は、Type&で宣言し、値型として受け取る。 | |
//RefはAと同じアドレスを指す変数 | |
Ref.a = 4; //Aのメンバーにアクセスもできる | |
//同じアドレスなので、当然 A.a == 4 | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int a; | |
int* p; //ポインタは、初期化なしで宣言できる | |
p = &a; //代入も可能 | |
//int& r; | |
//=ERROR= 参照は、初期化なしで宣言できない | |
int b; | |
int& r = a; | |
//r = b; | |
//=ERROR= 代入もできない |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment