Home > Article > Backend Development > Object-oriented topics in C and C++ (8) - a more advanced preprocessor PHP
List of articles in this column
1. What is object-oriented
2. C language can also implement object-oriented
3. Inelegant features in C++
4. Solve encapsulation and avoid interfaces
5. Reasonable use Templates to avoid code redundancy
6. C++ can also reflect
7. Singleton mode solves the construction order problem of static member objects and global objects
8. More advanced preprocessor PHP
8. More The advanced preprocessor PHP
C++ macros are very difficult to use in some cases. For example, the code is expanded like this:
Macro(A, B, C, D)
=>
func(“A ", A);
func(“B”, B);
func(“C”, C);
func(“D”, D);
test(A);
test(B);
test(C);
test(D);
This is too difficult for macros. In order to achieve complex macro expansion, we hope to use a more advanced preprocessor to implement this function.
We use PHP here to preprocess the code, and use the PHP code as a C++ macro.
Of course, you can also use Python for code generation, but since PHP is embedded, it may be more convenient to handle. Of course, other languages can also be equipped with templates.
<code><span>/* main.php */</span> <?php $return_m = <span>"return a + b;"</span> ?> <span>#include <iostream></span><span>using</span><span>namespace</span><span>std</span>; <span>int</span> func(<span>int</span> a, <span>int</span> b) { <?php echo $return_m; ?> } <span>int</span> main() { <span>cout</span> << func(<span>1</span>, <span>2</span>) << endl; <span>return</span><span>0</span>; }</code>
We use the following instructions to generate C++ code:
<code>php main.php > main.cpp </code>
Okay, the following is the same as normal project compilation. You can even write the php command into the makefile to automatically generate
The above has introduced the object-oriented topic of C and C++ (8) - the more advanced preprocessor PHP, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.