GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 22-Jan-2009, 04:11
hkn hkn is offline
New Member
 
Join Date: Jan 2009
Posts: 1
hkn is on a distinguished road

Selecting datatype at runtime


Hello,

is it possible to choose the type of variable at runtime in c++

for example

CPP / C++ / C Code:
void func(int x)
{
    if(x == 1)
    {
         double y;
    }
    elseif(x== 2)
    {
         int y;
    }
    else
    {
         float y;
    }
    
}

or actually I want to define a templated variable with different types according to the input in a function

CPP / C++ / C Code:
func(int x)
{
     if( x == 0)
          templatedvariable<int> y = new templatedvariable<int>();
     else
          templatedvariable<double> y = new templatedvariable<double>();
}
Last edited by admin : 22-Jan-2009 at 04:58. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #2  
Old 22-Jan-2009, 06:14
Mexican Bob's Avatar
Mexican Bob Mexican Bob is offline
Member
 
Join Date: Mar 2008
Location: Chicxulub, Yucatán
Posts: 226
Mexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the rough

Re: Selecting datatype at runtime


Template types are known at compile time and are probably not what you really want.

You may want to explore RTTI (runtime type information). However, it is unclear what you really want to do based on the information that you've provided. Your example, even if successful doesn't do anything meaningful, so it is useless. Useless in what it "does" (if it worked) and useless at conveying your real meaning.

If you mean to say that you want to pass in a variable that describes the "type information," to a function and have it return a new instance of the type, fine. What you've described can be implemented in a few different ways. the VARIANT type popular in Visual Basic is a good example of how to implement such a beast. In C it is implemented using a union of several types and, of course, an identifier that identifies the type to "the world."

How about rephrasing what you want and add perhaps more about what you want to do with it so that we'll better know how to respond?


MxB
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Runtime Error 21 n10doefrk Computer Software Forum - Windows 3 26-Sep-2008 04:56
How to Suppress Runtime Error Dialog Box js_63042 MS Visual C++ / MFC Forum 0 21-Jul-2008 14:30
How to change menu label at runtime suja FLTK Forum 4 10-Jul-2007 20:30
how to increase the font size at runtime banur22 MS Visual C++ / MFC Forum 1 21-Apr-2005 07:37
Free copy of Runtime Revolution crystalattice Open Discussion Forum 0 29-Dec-2004 16:52

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:31.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.