Some questions..

1- Is it possible to define a #define using lots of defines ?

Example:
#define c_a a
#define c_b b

#define CONDITION c_ac_b

// CONDITION must be "ab".

2- Is it possible to make a string(or char, etc.) like a code ?

Example:
string str = "exit(1);";

MAKECODE(str);

// str must be converted to a code.
Last edited on
1- Is it possible to define a #define using lots of defines ?


I don't understand the question.

 
2- Is it possible to make a string(or char, etc.) like a code ?


No it's not. It's possible in most (if not all) interpreted languages though.
1 - You can if you're dealing with string literals, as adjacent ones will be concatenated:
1
2
3
#define c_i "a"
#define c_f "b"
#define CONDITION c_i c_f 

Aside from that, you can concatenate tokens:
#define CONCAT(a,b) a ## b
However, CONCAT(c_i,c_f) would produce c_ic_f, not ab.

With another indirection, you can get the desired result:
1
2
3
4
5
6
7
#define c_i a
#define c_f b

#define CONCAT(a,b) a ## b
#define INDIR_CONCAT(a,b) CONCAT(a,b)

#define CONDITION INDIR_CONCAT(c_i,c_f) 


2 - No (unless you invoke a compiler with the code and load the DLL).
Last edited on
I don't understand the question.

There is a #define called CONDITION. I can it make like this:

#define CONDITION ab

But i want to do that using the other #define's.
Example:

#define A a
#define B b

#define CONDITION AB
Last edited on
For your first question, when I tried to compile this code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
1- Is it possible to define a #define using lots of defines ?

Example:
#include <iostream>
#define c_a a
#define c_b b

#define CONDITION c_ac_b

int main()
{
    CONDITION
}
// str must be converted to a code.  


It gave me an error saying
error: 'c_ac_b' was not declared in this scope


Which suggests that #define CONDITION c_ac_b doesn't define CONDITION as ab

EDIT:
But when I modified the program to
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#define c_a a
#define c_b b

#define CONDITION c_a

int main()
{
    CONDITION
}


The error this time said
error: 'a' was not declared in this scope|

Meaning that CONDITION was defined to a instead of c_a
Last edited on
Thank you all.
1
2
3
4
5
6
7
#define c_i a
#define c_f b

#define CONCAT(a,b) a ## b
#define INDIR_CONCAT(a,b) CONCAT(a,b)

#define CONDITION INDIR_CONCAT(c_i,c_f) 


This is working very well. First question has solved.
Last edited on
Now according to Athar, it is possible to make my last question using dlls. But how ?
Put the code inside a function and instruct the compiler to create a shared library:
g++ -shared [other flags] code.cpp -o temp.so[or .dll]

Now you can load the library and execute the function from your program using the appropriate functions (dlopen/dlsym or LoadLibrary/GetProcAddress).
OK, thank you.
Topic archived. No new replies allowed.