473,320 Members | 2,092 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,320 software developers and data experts.

selecting the type of an object into an if clause (howto?)

Hi all, I have this problem. My program should analyze some data and
it features a configuration file in which it's defined the data source
(could be plain text or MYSQL data). To do so I made two classes
with the same methods so that they can be called and used the same
way regardless of what they do.
But!! Here I have this problem, how to select the input without recompiling.
First tried...:
Expand|Select|Wrap|Line Numbers
  1. inputType = config.read("type");  // This reads the source type
  2. if (inputType=="text"){
  3.      classText* inputData = new classText(...)
  4. }else{
  5.      classMySQL* inputData = new classMySQL(...)
  6. }
  7. inputData->Analyse(..) // And then do all the stuff 
  8.  
This won't work, ok, cause inputData is defined into if scope, then tried to
declare void* inputData, declare both classes with different names and assign the right object to inputData into an if clause but I haven't succeeded..

Can you help???:):):)
thanks a lot, cheers
Nov 2 '07 #1
2 1273
Banfa
9,065 Expert Mod 8TB
If both classes have the same functions then they both have the same interface. This is a case of polymorphism and what you probably need to do is implement a base class that defines the interface using public non-virtual functions with provate (pure) virtual functions defining the methods each sub-class will using to provide there implementation.

A simple example of this is

Expand|Select|Wrap|Line Numbers
  1. #include <string>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. class Base
  8. {
  9. private:
  10.     virtual void OutputData() = NULL;
  11.  
  12. public:
  13.     void Print();
  14. };
  15.  
  16. void Base::Print()
  17.     OutputData(); 
  18. }
  19.  
  20.  
  21. class Integer : public Base
  22. {
  23. private:
  24.     int data;
  25.  
  26. private:
  27.  
  28.     virtual void OutputData()
  29.     {
  30.         cout << data << endl;
  31.     }
  32.  
  33.  
  34. public:
  35.     Integer(int init = 5)
  36.                 : data(init)
  37.     {
  38.     }
  39. };
  40.  
  41.  
  42.  
  43. class MyString : public Base
  44. {
  45. private:
  46.     string data;
  47.  
  48. private:
  49.     virtual void OutputData()
  50.     {
  51.         cout << data << endl;
  52.     }
  53.  
  54. public:
  55.     MyString(string &init = string("Hello World"))
  56.                 : data(init)
  57.     {
  58.     }
  59. };
  60.  
  61.  
  62. int main()
  63. {
  64.     static const bool WantInt = false;
  65.     Base *myType;
  66.  
  67.     if (WantInt)
  68.     {
  69.         myType = new Integer;
  70.     }
  71.     else
  72.     {
  73.         myType = new MyString;
  74.     }
  75.  
  76.     myType->Print();
  77.  
  78.     return 0;
  79. }
  80.  
Nov 2 '07 #2
I didn't know this property of polimorphism...wow, it's exactly what I needed.. and it works! :):):):):)
Thanks a lot!
Nov 2 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Paul Bradford | last post by:
I have a query that takes about two minutes and returns 97 rows. If I change the query only by adding ROWNUM to the outermost select clause, the query never returns (I let it run overnight). I'm...
6
by: eugenef | last post by:
We are running into the following issue with DB2 V8.1.3 on AIX 5: We have a sample table create table t ( c1 int, c2 varchar(20)) populated with some records: db2 => select * from t C1 ...
1
by: vunderusaf | last post by:
I have a listbox on a form that is selecting using named FinalQuery: SELECT ., ., . FROM FinalQuery; Now I have a text field with a date on this form and I'd like to use that date as the...
2
by: Paul Bradford | last post by:
I have a query that takes about two minutes and returns 97 rows. If I change the query only by adding ROWNUM to the outermost select clause, the query never returns (I let it run overnight). I'm...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
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...
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...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
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...
0
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

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.