crtp_cast

  • ATL/WTLではCRTPというテクニックを多用している
    • これはtemplateを使ったTemplate Methodパターンである(ややこしい)
  • static_castが使えるが意味が多すぎるので避けたい、そこで・・・ダウンロード
  • キャストさえ面倒な人にはauto_derived

[]#include[] []<pstade/crtp_cast.hpp>[]

[]template[][]< [][]class[] []Derived[][] >[]
[]struct[] []WindowBase[]
[]{[]
[]void[] []create[][]()[]
[] {[]
[]pstade[][]::[][]crtp_cast[][]<[][]Derived[][]>([][]this[][]).[][]create_impl[][]();[]

[]Derived[][]& [][]d[][] = [][]pstade[][]::[][]auto_derived[][]([][]this[][]);[]
[]d[][].[][]create_impl[][]();[]

[]Derived[][]* [][]pd[][] = [][]pstade[][]::[][]auto_derived[][]([][]this[][]);[]
[]pd[][]->[][]create_impl[][]();[]
[] }[]

[]void[] []show[][]() [][]const[]
[] {[]
[]pstade[][]::[][]crtp_cast[][]<[][]const[] []Derived[][]>([][]this[][]).[][]show_impl[][]();[]

[]const[] []Derived[][]& [][]d[][] = [][]pstade[][]::[][]auto_derived[][]([][]this[][]);[]
[]d[][].[][]show_impl[][]();[]

[]const[] []Derived[][]* [][]pd[][] = [][]pstade[][]::[][]auto_derived[][]([][]this[][]);[]
[]pd[][]->[][]show_impl[][]();[]
[] }[]
[]};[]

[]struct[] []Window[][] : [][]WindowBase[][]< [][]Window[][] >[]
[]{[]
[]void[] []create_impl[][]()[]
[] {[]
[]pstade[][]::[][]print[][]([][]"Window::create_impl"[][]);[]
[] }[]

[]void[] []show_impl[][]() [][]const[]
[] {[]
[]pstade[][]::[][]print[][]([][]"Window::show_impl"[][]);[]
[] }[]
[]};[]

[]struct[] []Dialog[][] : [][]WindowBase[][]< [][]Dialog[][] >[]
[]{[]
[]void[] []create_impl[][]()[]
[] {[]
[]pstade[][]::[][]print[][]([][]"Dialog::create_impl"[][]);[]
[] }[]

[]void[] []show_impl[][]() [][]const[]
[] {[]
[]pstade[][]::[][]print[][]([][]"Dialog::show_impl"[][]);[]
[] }[]
[]};[]