1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
#include <iostream>
#include <vector>
struct base
{
struct vec2d // wrapper class to hold the vector
{
std::vector< std::vector<int> > data ;
// this operator can be found only via ADL (good idea)
friend std::ostream& operator<< ( std::ostream& stm, const vec2d& v2d )
{
for( const auto& row : v2d.data )
{
for( int v : row ) stm << v << ' ' ;
stm << '\n' ;
}
return stm << "\n\n" ;
}
};
// Guideline #1: Prefer to make interfaces non-virtual
// see: http://www.gotw.ca/publications/mill18.htm
vec2d some_func() const { return some_func_impl() ; }
void print_some_func_result() const { std::cout << some_func() << '\n' ; }
// ...
// Guideline #2: Prefer to make virtual functions private
// see: http://www.gotw.ca/publications/mill18.htm
private: virtual vec2d some_func_impl() const
{ return { { {1,2,3}, {4,5,6}, {7,8,9} } } ; }
};
struct derived : base
{
private: virtual vec2d some_func_impl() const override
{ return { { {1,2,3,4}, {5,6,7,8}, {2,3,4,5}, {6,7,8,9} } } ; }
// ...
};
int main()
{
const base bb ;
const derived dd ;
const base& b1 = bb ;
const base& b2 = dd ;
b1.print_some_func_result() ;
b2.print_some_func_result() ;
}
|