Last active
August 29, 2015 14:08
-
-
Save gummikana/1bae626cf12e2926f920 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <vector> | |
| template< class T > | |
| class Storage | |
| { | |
| public: | |
| Storage() { } | |
| template< class T2 > | |
| Storage( const Storage<T2>& other_c ) { operator=( other_c ); } | |
| ~Storage() { } | |
| template< class T2 > | |
| const Storage<T>& operator=( const Storage<T2>& other_c ) | |
| { | |
| mData.resize( other_c.mData.size() ); | |
| for( std::size_t i = 0; i < other_c.mData.size(); ++i ) | |
| { | |
| mData[i] = (T)other_c.mData[i]; | |
| } | |
| return *this; | |
| } | |
| int size() const { return (int)mData.size(); } | |
| T& operator[](int i ) { return mData[i]; } | |
| const T& operator[](int i ) const { return mData[i]; } | |
| void resize( int new_size ) { mData.resize( new_size ); } | |
| void push_back( const T& element ) { mData.push_back( element ); } | |
| std::vector< T > mData; | |
| }; | |
| // ---- test case implementation stuff ------------------ | |
| void test_function( const Storage< float >& floats ) | |
| { | |
| for( int i =0; i < floats.size(); ++i ) | |
| { | |
| std::cout << i << " : " << floats[i] << std::endl; | |
| } | |
| } | |
| // derived case | |
| class Base { | |
| public: virtual void call_me() { std::cout << "base::call_me()" << std::endl; } | |
| }; | |
| class Derived : public Base { | |
| public: virtual void call_me() { std::cout << "Derived::call_me()" << std::endl; } | |
| }; | |
| void test_function2( const Storage< Base* >& bases ) | |
| { | |
| for( int i =0; i < bases.size(); ++i ) | |
| { | |
| bases[i]->call_me(); | |
| } | |
| } | |
| // storage inside a storage | |
| void test_function3( const Storage< Storage< Derived* > >& bases_again ) | |
| { | |
| for( int i = 0; i < bases_again.size(); ++i ) | |
| { | |
| for( int j = 0; j < bases_again[i].size(); ++j ) | |
| { | |
| bases_again[i][j]->call_me(); | |
| } | |
| } | |
| } | |
| int main( int args, char** argc ) | |
| { | |
| Storage< int > ints; | |
| ints.push_back( 1 ); | |
| ints.push_back( 2 ); | |
| ints.push_back( 3 ); | |
| ints.push_back( 4 ); | |
| test_function( ints ); | |
| Storage< Derived* > derived; | |
| derived.push_back( new Derived ); | |
| derived.push_back( new Derived ); | |
| derived.push_back( new Derived ); | |
| derived.push_back( new Derived ); | |
| test_function2( derived ); | |
| std::cout << "---" << std::endl; | |
| Storage< Storage< Base* > > storage_storage; | |
| storage_storage.push_back( derived ); | |
| storage_storage.push_back( derived ); | |
| storage_storage.push_back( derived ); | |
| test_function3( storage_storage ); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment