Thread: C++ Runtime Decisions

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214

    C++ Runtime Decisions

    Hello,

    suppose i have something like this

    Code:
    class Storage
    {
    public:
    	Storage(int storageType)
    	{
    		switch(storageType)
    		{
    			case TYPE1:
    				data = new DerivedType1();
    				break;
    			case TYPE2:
    				data = new DerivedType2();
    				break;
    		}
    	}
    
    	~Storage(void)
    	{
    		delete data;
    	}
    
    private:
    	BaseType *data;
    };
    What i don't like about this is that it is decided at runtime which type is allocated. Is doing this common, or is there any way to do this better? I mean i could use an enumeration for the storageType.

    As i know the type at compiletime, i could use templates to specify the type, which would be a class.

    What is better from a design perspective, runtime decision or using templates?

    Best regards

    EDIT: figured out how to pass classes as types to templates
    Last edited by threahdead; 05-06-2011 at 08:51 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Runtime formation and execution at runtime
    By Soham in forum C Programming
    Replies: 17
    Last Post: 08-27-2008, 08:45 AM
  2. search algorithm - making statistical decisions
    By Mario F. in forum C++ Programming
    Replies: 3
    Last Post: 11-18-2006, 10:03 AM
  3. conio linux and decisions
    By curlious in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 09-09-2005, 01:49 PM
  4. overloaded *operator, decisions, and loops
    By ronin in forum C++ Programming
    Replies: 2
    Last Post: 02-12-2003, 10:22 AM
  5. Moderator decisions
    By Series X4 1.0 in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 12-13-2001, 10:57 PM