Click here to Skip to main content
15,915,600 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: vector of template class Pin
forwardsim18-Aug-10 10:08
forwardsim18-Aug-10 10:08 
AnswerRe: vector of template class Pin
Aescleal18-Aug-10 10:10
Aescleal18-Aug-10 10:10 
GeneralRe: vector of template class Pin
forwardsim18-Aug-10 10:34
forwardsim18-Aug-10 10:34 
GeneralRe: vector of template class Pin
Aescleal18-Aug-10 11:11
Aescleal18-Aug-10 11:11 
GeneralRe: vector of template class Pin
forwardsim18-Aug-10 11:02
forwardsim18-Aug-10 11:02 
AnswerRe: vector of template class Pin
Niklas L18-Aug-10 10:30
Niklas L18-Aug-10 10:30 
GeneralRe: vector of template class [modified] Pin
forwardsim18-Aug-10 10:54
forwardsim18-Aug-10 10:54 
GeneralRe: vector of template class Pin
Aescleal18-Aug-10 11:40
Aescleal18-Aug-10 11:40 
I probably shouldn't encourage this - heterogeneous collections usually means there's something a bit screwy in your design. But where OO and generic programming collide you can use runtime type information:

class test
{
    public:
        virtual test &operator+=( const test &other ) = 0;
};

template <typename T>
class T_test : public test
{
    public:
	T_test( T initial ) : value_( initial )
	{
	}

        virtual test &operator+=( const test &other )
        {
		if( const T_test<T> *p = dynamic_cast<const T_test<T> *>( &other ) )
		{
			value_ += p->value_;
		}
		return *this;
        }

	private:
	    T value_;
};


Then you can use it something like:

int main()
try
{
	T_test<int> a( 100 );
	T_test<double> b( 100.0 );
	T_test<int> c( 200 );

	test *ptrs[] = { &a, &b, &c };
	std::vector<test *> all( ptrs, ptrs + 3 );

	T_test<int> result( 0 );

	for( std::vector<test *>::const_iterator iter = all.begin(); iter != all.end(); ++iter )
	{
		result += **iter;
	}
}
catch( const std::exception &e )
{
	std::cout << e.what() << std::endl;
}
catch( ... )
{
	std::cout << "Something went wrong, no idea what!" << std::endl;
}


If this does the sort of thing you're after please consider how to get rid of the dynamic_cast. Oh, and if anyone asks who told you to do this please don't mention my name, tell them it's some other random hacker.

Cheers,

Ash

Edited for tabs and adding the catch(...)
GeneralRe: vector of template class Pin
forwardsim18-Aug-10 12:22
forwardsim18-Aug-10 12:22 
GeneralRe: vector of template class Pin
Aescleal18-Aug-10 22:15
Aescleal18-Aug-10 22:15 
GeneralRe: vector of template class Pin
Emilio Garavaglia18-Aug-10 22:23
Emilio Garavaglia18-Aug-10 22:23 
GeneralRe: vector of template class Pin
forwardsim19-Aug-10 4:44
forwardsim19-Aug-10 4:44 
AnswerRe: vector of template class Pin
Niklas L19-Aug-10 2:23
Niklas L19-Aug-10 2:23 
GeneralRe: vector of template class Pin
Aescleal19-Aug-10 3:01
Aescleal19-Aug-10 3:01 
GeneralRe: vector of template class Pin
Niklas L19-Aug-10 3:16
Niklas L19-Aug-10 3:16 
GeneralRe: vector of template class Pin
forwardsim19-Aug-10 4:47
forwardsim19-Aug-10 4:47 
GeneralRe: vector of template class Pin
Aescleal19-Aug-10 6:24
Aescleal19-Aug-10 6:24 
GeneralRe: vector of template class Pin
Niklas L19-Aug-10 8:29
Niklas L19-Aug-10 8:29 
GeneralRe: vector of template class [modified] Pin
Paul Michalik20-Aug-10 2:03
Paul Michalik20-Aug-10 2:03 
GeneralRe: vector of template class Pin
Niklas L20-Aug-10 2:22
Niklas L20-Aug-10 2:22 
GeneralRe: vector of template class Pin
Paul Michalik20-Aug-10 4:06
Paul Michalik20-Aug-10 4:06 
Questioncan main() be overloaded?? Pin
AmbiguousName18-Aug-10 6:36
AmbiguousName18-Aug-10 6:36 
AnswerRe: can main() be overloaded?? Pin
Aescleal18-Aug-10 6:50
Aescleal18-Aug-10 6:50 
QuestionRe: can main() be overloaded?? Pin
David Crow18-Aug-10 9:14
David Crow18-Aug-10 9:14 
AnswerRe: can main() be overloaded?? Pin
Aescleal18-Aug-10 10:00
Aescleal18-Aug-10 10:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.