interface_example.cpp

来自「Boost provides free peer-reviewed portab」· C++ 代码 · 共 81 行

CPP
81
字号
// (C) Copyright Tobias Schwinger//// Use modification and distribution are subject to the boost Software License,// Version 1.0. (See http://www.boost.org/LICENSE_1_0.txt).//------------------------------------------------------------------------------// See interface.hpp in this directory for details.#include <iostream>#include <typeinfo>#include "interface.hpp"BOOST_EXAMPLE_INTERFACE( interface_x,  (( a_func, (void)(int) , const_qualified ))  (( a_func, (void)(long), const_qualified ))  (( another_func, (int) , non_const   )) );// two classes that implement interface_xstruct a_class{  void a_func(int v) const  {    std::cout << "a_class::void a_func(int v = " << v << ")" << std::endl;  }  void a_func(long v) const  {    std::cout << "a_class::void a_func(long v = " << v << ")" << std::endl;  }  int another_func()  {    std::cout << "a_class::another_func() = 3" << std::endl;    return 3;  } };struct another_class{  // note: overloaded a_func implemented as a function template  template<typename T>  void a_func(T v) const  {    std::cout <<       "another_class::void a_func(T v = " << v << ")"       "  [ T = " << typeid(T).name() << " ]" << std::endl;  }  int another_func()  {    std::cout << "another_class::another_func() = 5" << std::endl;    return 5;  } };// both classes above can be assigned to the interface variable and their// member functions can be called through itint main(){  a_class x;  another_class y;  interface_x i(x);  i.a_func(12);  i.a_func(77L);  i.another_func();  i = y;  i.a_func(13);  i.a_func(21L);  i.another_func();}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?