📢 Webサイト閉鎖と移転のお知らせ
このWebサイトは2026年9月に閉鎖いたします。
新しい記事は移転先で追加しております。(旧サイトでは記事を追加しておりません)

Wiki がページ「C++と4つのキャスト(C++)」を「C++の基礎 - キャスト」に、リダイレクトを残さずに移動しました
文字列「__FORCETOC__」を「{{#seo: |title={{PAGENAME}} : Exploring Electronics and SUSE Linux | MochiuWiki |keywords=MochiuWiki,Mochiu,Wiki,Mochiu Wiki,Electric Circuit,Electric,pcb,Mathematics,AVR,TI,STMicro,AVR,ATmega,MSP430,STM,Arduino,Xilinx,FPGA,Verilog,HDL,PinePhone,Pine Phone,Raspberry,Raspberry Pi,C,C++,C#,Qt,Qml,MFC,Shell,Bash,Zsh,Fish,SUSE,SLE,Suse Enterprise,Suse Linux,openSUSE,open SUSE,Leap,Linux,uCLnux,Podman,電気回路,電子回路,基板,プリント基板 |description={{PAGENAME}} - 電子回路とSUSE Linuxに関する情報 | This pag…
 
(同じ利用者による、間の2版が非表示)
1行目: 1行目:
== static_cast ==
== static_cast ==
ある型からある型への暗黙の変換が存在する時(例 : int型からdouble型へ変換)、そこで暗黙の変換が行われることを明示する場合に使用する。<br>
ある型からある型への暗黙の変換が存在する時(例 : int型からdouble型へ変換)、そこで暗黙の変換が行われることを明示する場合に使用する。<br>
  <source lang="c++">
  <syntaxhighlight lang="c++">
  double d = 3.14;
  double d = 3.14;
  int i = static_cast<int>(d);
  int i = static_cast<int>(d);
  </source>
  </syntaxhighlight>
<br><br>
<br><br>


12行目: 12行目:
ポインタが指す実体が、キャスト先のクラスまたはその子孫クラスである場合はポインタはそのまま使われるが、<br>
ポインタが指す実体が、キャスト先のクラスまたはその子孫クラスである場合はポインタはそのまま使われるが、<br>
キャスト先のクラスとして使用できない場合には、NULL(nullptr)に置き換えられる。<br>
キャスト先のクラスとして使用できない場合には、NULL(nullptr)に置き換えられる。<br>
  <source lang="c++">
  <syntaxhighlight lang="c++">
  Child0 child00;
  Child0 child00;
  Parent* parent = &child0;
  Parent* parent = &child0;
26行目: 26行目:
  Child2* child2 = dynamic_cast<Child2*>(parent);
  Child2* child2 = dynamic_cast<Child2*>(parent);
  std::cout << "child2 : " << child2 << std::endl;
  std::cout << "child2 : " << child2 << std::endl;
  </source>
  </syntaxhighlight>
<br>
<br>
dynamic_castを行うためには、型情報がポインタから得られる必要がある。<br>
dynamic_castを行うためには、型情報がポインタから得られる必要がある。<br>
37行目: 37行目:
dynamic_castと違うのは、ポインタの型変換が安全に行えるかは考慮されない。<br>
dynamic_castと違うのは、ポインタの型変換が安全に行えるかは考慮されない。<br>
また、整数型(int、long、long long等)を任意の型のポインタに変換するのにも利用できる。<br>
また、整数型(int、long、long long等)を任意の型のポインタに変換するのにも利用できる。<br>
  <source lang="c++">
  <syntaxhighlight lang="c++">
  int i;
  int i;
  char *c = reinterpret_cast<char*>(&i);
  char *c = reinterpret_cast<char*>(&i);
47行目: 47行目:
  long addrint = reinterpret_cast<long>(c);
  long addrint = reinterpret_cast<long>(c);
  std::cout << "addrint : " << addrint << std::endl;
  std::cout << "addrint : " << addrint << std::endl;
  </source>
  </syntaxhighlight>
<br><br>
<br><br>


55行目: 55行目:
互換性の問題でどうしても使わざるを得ない場合以外では使うべきではない。<br>
互換性の問題でどうしても使わざるを得ない場合以外では使うべきではない。<br>
<br><br>
<br><br>
{{#seo:
|title={{PAGENAME}} : Exploring Electronics and SUSE Linux | MochiuWiki
|keywords=MochiuWiki,Mochiu,Wiki,Mochiu Wiki,Electric Circuit,Electric,pcb,Mathematics,AVR,TI,STMicro,AVR,ATmega,MSP430,STM,Arduino,Xilinx,FPGA,Verilog,HDL,PinePhone,Pine Phone,Raspberry,Raspberry Pi,C,C++,C#,Qt,Qml,MFC,Shell,Bash,Zsh,Fish,SUSE,SLE,Suse Enterprise,Suse Linux,openSUSE,open SUSE,Leap,Linux,uCLnux,Podman,電気回路,電子回路,基板,プリント基板
|description={{PAGENAME}} - 電子回路とSUSE Linuxに関する情報 | This page is {{PAGENAME}} in our wiki about electronic circuits and SUSE Linux
|image=/resources/assets/MochiuLogo_Single_Blue.png
}}


__FORCETOC__
__FORCETOC__
[[カテゴリ:C++]]
[[カテゴリ:C++]]