[英]How to convert these lines of c code to c++
I'm trying to import some c code into my c++ program. 我正在尝试将一些C代码导入我的C ++程序。 There are three lines that don't import directly:
有三行不直接导入:
The first: 首先:
free(t);
The second: 第二:
new_node = (Tree *) malloc (sizeof (Tree));
The third: 第三:
Tree * delete(int value, Tree * t)
How can these be changed to work in C++? 这些如何更改才能在C ++中工作?
You can use free and malloc in C++. 您可以在C ++中使用free和malloc。 Whether you should is a different story, but if you're porting a C library the answer to that is yes (at least for now).
是否您应该是一个不同的故事,但是如果您要移植C库,那么答案是肯定的(至少目前如此)。
delete
is a keyword in C++, you will need to rename that function. delete
是C ++中的关键字,您需要重命名该函数。
Assuming you want convert it to C++ style new/delete (see other answers about continuing to use malloc/free): 假设您要将其转换为C ++样式的new / delete(请参见有关继续使用malloc / free的其他答案):
// 1. free(t);
delete t;
// 2. new_node = (Tree *) malloc (sizeof (Tree));
new_node = new Tree;
// 3. Tree * delete(int value, Tree * t)
Tree * delete_tree(int value, Tree* t)
Note: for #3, you will need to change all users of delete(value, t)
to delete_tree(value, t)
. 注意:对于#3,您需要将所有
delete(value, t)
用户更改为delete_tree(value, t)
。
The first two lines should be valid C++, assuming you've included stdlib.h, and have defined Tree as a class/struct/type somewhere. 前两行应该是有效的C ++,假设您已包含stdlib.h,并且已将Tree定义为某处的类/结构/类型。
The third line will need to be changed, since 'delete' is a keyword in C++ and can't be used as a function name. 第三行需要更改,因为“ delete”是C ++中的关键字,不能用作函数名。 Try doing a global replace in the C code and changing all instances of 'delete' with 'delete_from_tree' or something like that.
尝试在C代码中进行全局替换,然后将所有'delete'实例更改为'delete_from_tree'或类似的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.