Skip to content

Instantly share code, notes, and snippets.

@hmito
Last active June 14, 2020 22:07
Show Gist options
  • Save hmito/ae0920588df8081b6f44 to your computer and use it in GitHub Desktop.
Save hmito/ae0920588df8081b6f44 to your computer and use it in GitHub Desktop.
それ、ポインタ使わなくてもできるよ:C言語のポインタとC++の流儀 ref: https://qiita.com/hmito/items/44925fca9fca74e78f61
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
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();
}
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;
}
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