473,322 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

selecting a type at run time?

Hi everyone,

Probably the solution to this question is trivial but I couldn't find
a clean way to do it. I try to have a variable depending on a
parameter that is read from a file at run time. Let's say something
like this:

....

dim = read(filename);
if(dim == 2)
G2D* g_ = new G2D();
else if (dim == 3)
G3D* g_ = new G3D();

// then use g_ to whatever is needed
....

The only way I managed to do this, is to have two pointers in the
code...=/
Is there a better approach? Thank you,



Sep 28 '07 #1
2 1109
aaragon wrote:
dim = read(filename);
if(dim == 2)
G2D* g_ = new G2D();
else if (dim == 3)
G3D* g_ = new G3D();

// then use g_ to whatever is needed
Google for the Factory Pattern in C++.

--
Phlip
http://www.oreilly.com/catalog/9780596510657/
^ assert_xpath

Sep 28 '07 #2
aaragon wrote:
Hi everyone,

Probably the solution to this question is trivial but I couldn't find
a clean way to do it. I try to have a variable depending on a
parameter that is read from a file at run time. Let's say something
like this:

...

dim = read(filename);
if(dim == 2)
G2D* g_ = new G2D();
else if (dim == 3)
G3D* g_ = new G3D();

// then use g_ to whatever is needed
...

The only way I managed to do this, is to have two pointers in the
code...=/
Is there a better approach?
Yes. Use polymorphism. Derive G2D and G3D from a polymorphic base class.

Sep 29 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: webhigh | last post by:
I¹m not sure if this a PHP question or an MySQL question but here it goes. I have a repeat region of a table called userid What I¹m trying to accomplish is being able to edit the record and...
3
by: Simon G Best | last post by:
Hello! The C++ standard library provides facilities for finding out the sizes (and other such stuff) of numeric types (::std::numeric_limits<>, for example). What I would like to do is to...
5
by: Axial | last post by:
Question: How to select columns from Excel-generated XML when some cells are empty. I've found examples where rows are to be selected, but I can't seem to extrapolate from that to selecting...
0
by: brent | last post by:
Hi there... I have a stateless Session Bean issuing a select for a BLOB column. This bean is using a CachedRowSet. It seems to work for all other columns types other than a BLOB. Selecting a...
3
by: klawiter | last post by:
Greetings, I have a table with two columns. On my form, I have a combo field that displays the contents of the first column. Upon selecting an item in this combo field, I would like to then...
1
by: Ramesh | last post by:
hi, I am selecting fields from three table for manupulating data and i want to display total number of records selected. But i am always getting -1 value, eventhough 1000 of records are selected....
6
by: aaj | last post by:
Hi all I use a data adapter to read numerous tables in to a dataset. The dataset holds tables which in turn holds full details of the records i.e. keys, extra colums etc.. In some cases I...
3
by: larry mckay | last post by:
anyone have the code to select and listview item or row (subitems) after a doubleclick event from a listview. *** Sent via Developersdex http://www.developersdex.com *** Don't just participate...
5
by: megahurtz | last post by:
I need to put together an SQL statement and I can't think of how to make it work properly. The scenario is that I have news items in a database that have a launch time and can optionally have an...
2
by: megahurtz | last post by:
I need to put together an SQL statement and I can't think of how to make it work properly. The scenario is that I have news items in a database that have a launch time and can optionally have an...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.