Solved

switching variable types in the same code

Posted on 1998-06-09
6
173 Views
Last Modified: 2010-04-01
(Visual C++) if I am writing a database, and I want the user to be able to search based on different fields, is there a way to write the code once with a variable that can change?  For example: I'd like then to be able to search for int ID, char name, or int age - all under the same class.  I have a statement (in a member function) "if(targetID != searchptr -> ID)..... "  I want to be able to use that same statement for the different fields without writing the code 3 different times.  Can I set up a variable that can be set to int ID, char name, etc based on what the user wants and just put that variable in my code?  This question is from someone learning C++, so answers can't be too vague...  
0
Comment
Question by:Reid
  • 3
  • 3
6 Comments
 

Author Comment

by:Reid
ID: 1165528
Edited text of question
0
 
LVL 22

Accepted Solution

by:
nietod earned 150 total points
ID: 1165529
Unfortunately, the question is a little vague...  but I think I know what you need.  Answer coming.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1165530
Now that I think about it.  There are two possible ways to go here.  

One is templates, but I suspect that is not what you really need.   Templates allow you to write algorithms (code) that are independant of data types, then the templates are "expanded" or converted into code for the data types you need.  For example you cou might want to create a max function that returns the maximum of two integers and another that returns the maximum of two doubles.  The two functions have same algorithm, they just work with different types of data.  You can write the function one time as a template and the compiler will create a version for integers and a second one for doubles.

The other option is to place you field types in a class hierarchy.  This is more advanced programming, But I suspect it is what you really need.  You would define an interface in the base class that allows you to compare types.  This is going over your head isn't it?

I'll tell you what, can you try to give me a better idea of what you've got or need?  If you could post some code or e-mail it to me at nietod@theshop.net that would be great.  From there I'll try to give you an idea what direction to go in.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 22

Expert Comment

by:nietod
ID: 1165531
I'm not pesterig you.  I'm just posting this to keep the question active.  Inactive questions are "autograded" after 21 days.  This comment us an "extension".

Hope the studying is going well.  
0
 

Author Comment

by:Reid
ID: 1165532
Adjusted points to 150
0
 

Author Comment

by:Reid
ID: 1165533
Thanks!
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now