Thread: C++ Runtime Decisions

  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.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Sounds like this might be a good case for an abstract factory pattern.

  3. #3
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    Quote Originally Posted by rags_to_riches View Post
    Sounds like this might be a good case for an abstract factory pattern.
    Thats pretty much what i need to do.
    But you're saying i should do it at runtime, although i know the types at compile time?

    What i have in mind is something like this
    Code:
    template<class Type>
    class Storage
    {
    public:
        Storage()
        {
            data = new Type();
        }
    private:
        Type *data
    };
    Last edited by threahdead; 05-06-2011 at 09:19 AM.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    We don't know what you are trying to do. Perhaps you should describe it in more detail.
    Generally speaking, if you know the type at compile time, doing it at compile time is usually a better idea.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    What you have shown appears to be a parameterized factory pattern where the type of object created depends on the parameter passed in. It is a valid pattern but I do not prefer it because it requires a type or some variable that indicates type which then necessitates a bunch of if's or a switch statement. The aforementioned abstract factory pattern might be a better approach. Also I would not have the constructor of any object act as the factory. You should prefer that the constructor would call out to the factory and then save off the result. The factory should really be independent of the classes that are using the products of the factory. This also allows other objects to take advantage of the factory should they need similar objects created for them.

    The constructor call would then become:

    Code:
    m_pData = SomeFactory.CreateObject(someType);
    if (!m_pData)
    {
        throw std::exception("Could not create object");
    }
    Since you are doing this in a constructor the only way to notify if an error occurred is to throw. You might want to re-think that as well. Note that this code assumes MSVC new behavior which returns 0 when the memory cannot be allocated. If I remember correctly this is not standard compliant and standard new throws std::bad_alloc when it cannot allocate which is what you would then catch in the constructor instead of checking the pointer.
    Last edited by VirtualAce; 05-07-2011 at 09:57 AM.

  6. #6
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    Thanks for all your input, it helped me alot!

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