473,394 Members | 1,965 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,394 software developers and data experts.

How to create variables by using macros

4
Hi every one,
I got this problem, i need to create classes in c++ by using macros.
Thats what i want:

#define createObject(type) type a;

main(){
string className = "int";
createObject(className);
}

unfortunately it doesnt works with strings i can do it only if i will use actual data type:

createObject(int);
___________________________________
currently i am using VS2005, well of course in theory i can use a lot of if statements or switch, but buy using this method i dont need to add code each time i want to add compatability for some new class.

Thank you very much !
Mar 26 '08 #1
5 2431
Laharl
849 Expert 512MB
You could also use templates/generic programming. They are finicky, though.
Mar 26 '08 #2
2di
4
I dont think tempaltes will solve all my problems.
I am sorry i didnt mentioned it at first time,
there is another thing i need to do:
beside just creating variables of different type, i need to access member variables of some classes by using strings.

here is example of what i need:

Expand|Select|Wrap|Line Numbers
  1. class ClassA
  2. {
  3. public: 
  4.     ClassA(){}
  5.     ~ClassA(){}
  6.     int positionX;
  7.     int positionY;
  8. };
  9.  
  10. #define M_test(A)  myClass.A  = 10;
  11.  
  12.  
  13. int _tmain(int argc, _TCHAR* argv[])
  14. {
  15.  
  16.     ClassA myClass;
  17.     M_test("positionX"); 
  18.  
  19.     cout<<myClass.positionX;
  20.     system("PAUSE");
  21.     return 0;
  22. }
M_test("positionX"); -> working only in this way -> M_test(positionX);
but i have all my classes and member function/variables stored as a string, so i need some how to convert ("string") into (string) ;(
Mar 26 '08 #3
Banfa
9,065 Expert Mod 8TB
but i have all my classes and member function/variables stored as a string
Why? Or may be more pertinently where did this data come from?

If this is the result of some external generation process that you are having to import then I would suggest you need to create a type (class) that can do the work for you.

I map of instances of the class will easily enable you to translate from "variable Name" to instance of variable. In turn the class could contain a map of a class representing the members of the class. This class could be a virtual base class and the actual types can be classes derived from this type.

In this way you can map variable names in strings and member names in strings to actual instances of data.

However I would only even think about/examine the possibilities of this approach if you are dealing with data generated by an external source that is like to change regularly.

If you are not dealing with data of this type start by looking at how you may reformat the data in these strings.

Oh and BTW any solution that relies on macros in C++ is almost certainly poorly designed in most cases.
Mar 26 '08 #4
2di
4
Quote:
Originally Posted by 2di
but i have all my classes and member function/variables stored as a string
Why? Or may be more pertinently where did this data come from?
Ok the story is, I writing my scripting language, At a moment langage is mostly completed, so i working on the c++ classes support inside of the scripting language, U create your classes in C++, and using them inside of ur script.

I have special class ObjectContainer which can store pointers to different Objects
It looks like that:
Expand|Select|Wrap|Line Numbers
  1.     void* _class;
  2.     ClassType _type;
  3.     string _name;
  4.     string _className;
Virtual machine has list of ObjectContainers, where each element represents some object which been created inside of the script;

every time its executes script line like that:

Expand|Select|Wrap|Line Numbers
  1. player.positionX = 10;
1)it searches ObjectContainer List for the "player" object
2)casting void* p into PlayerClass*
3)changing value to 10

This is a code i have at a moment

Expand|Select|Wrap|Line Numbers
  1. void AssignToken(void* vClassContainer, string dataM, Token* token)
  2.     {
  3.         ClassContainer* container = reinterpret_cast<ClassContainer*>(vClassContainer);
  4.         if (container->_type == ct_Player)
  5.         {
  6.             Player* object = reinterpret_cast<Player*>(container->_class);
  7.             if (dataM == "positionX")
  8.             {
  9.                 object->PositionX = token->intData();
  10.             }
  11.         }
  12.     } 
where:
vClassContainer pointerto ObjectContainer;
dataM = "positionX"
token = pointer to a token with value (10);
this means that every time i am going to add new class, i need to write all this stupid if statements.

And this is what i want:

Expand|Select|Wrap|Line Numbers
  1. #define M_cast(className)  className* object = reinterpret_cast<className*>(container->_class);
  2. #define M_assignToken(dataMemberName) object->dataMemberName = token->intData();
  3.  
  4. void AssignToken(void* vClassContainer, string dataM, Token* token)
  5.     {
  6.  
  7.         ClassContainer* container = reinterpret_cast<ClassContainer*>(vClassContainer);
  8.         string className = container->_className;
  9.  
  10.         M_cast(vClassContainer->_className);
  11.         M_assignToken(dataM);
  12.     }
Target of this scripting language is a AI engine, so this language has to be easily integrated. So all i am trying to do is to make integration of new classes as simple as possible.
classes data, wont be genereated more than once, for one particular project.

Thanks for the idea about maps, i am thinking about it at a moment.
Mar 27 '08 #5
2di
4
I map of instances of the class will easily enable you to translate from "variable Name" to instance of variable. In turn the class could contain a map of a class representing the members of the class. This class could be a virtual base class and the actual types can be classes derived from this type.
could you please show me some code examples of this one ?
Mar 27 '08 #6

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

Similar topics

3
by: Dariusz | last post by:
I have a problem where I need to pass two variables using GET from a form I have, to solve a page selection problem I have. The code is written that if a new visitor arrives at the front page of...
4
by: Benjamin | last post by:
Hi! I would like to dim variables using dynamic code, like following: Dim type as String = "Integer" Dim variable as (the issue is in fact about my own types, but let us isolate the issue...
6
by: Shibu | last post by:
Hi, Can anyone tell me how to create a DSN from asp.net.. Thanks Shibu
0
by: Lopamudra | last post by:
Hi, I have a doubt: Any key defined in the "appsettings" section of the web.config file is global to the application. Each time the web.config is changed, the application restarts. Both...
1
by: greek_bill | last post by:
Hi all, I'd like to have code that looks like the following : BEGIN(Foo) ADD(float); ADD(int); ADD(char); END()
5
by: reon | last post by:
While we can create files using c++... Is there any code relating to create folders in c++..
5
bilibytes
by: bilibytes | last post by:
hi, i was wondering how to create variables on the fly, by this i mean: i have a pair of strings or an associative array with one string for Key and another for Value: $var1 = 'hello'; $var2...
6
by: nicolenwn | last post by:
Hi everyone(: I'm having trouble creating pivot tables using macros. First i tried recording it then running the exact same thing. It worked fine the first time but a week later when i tried...
0
by: ramilol | last post by:
i wanna create cursor but i have no idea how all i know is that ill use create cursor to do it but the last two paremters need a some 00xff values i have no idea how to use them can't i just draw...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.