返回const char *;(Returning const char*; how ugly is static?)

编程入门 行业动态 更新时间:2024-10-23 01:32:39
返回const char *;(Returning const char*; how ugly is static?)

由于我无法控制的原因,我需要从一个函数中返回const char* ,但我不知道编译时char需要什么。 我的解决方案如下所示:

const char* __str__() { static std::string String; String = [some fancy stuff]; return String.c_str(); }

static可以防止字符串在退出函数时的破坏,但这也意味着内存会一直存在,直到我的程序退出(正确?)。 由于返回的字符串偶尔会很大 (GB),这可能是一个真正的问题。

我通常不惜一切代价避免指针,并且只对类成员使用static ,所以我不能100%确定我在做什么。 这是保证工作吗? 有没有更好的办法?

[这个问题的上下文是用python打印一个复杂的对象,使用__str__方法。 我在c ++代码中定义了方法,然后由SWIG包装。 SWIG示例显示了static的使用,但我不清楚这是唯一的方法。 我愿意接受建议。]

For reasons beyond my control, I need to return const char* from a function, but I don't know what the chars need to be at compile time. My solution is something like the following:

const char* __str__() { static std::string String; String = [some fancy stuff]; return String.c_str(); }

The static prevents the string's destruction on exiting the function, but it also means that the memory sticks around until my program exits (right?). Because the returned string can occasionally be huge (GBs), this can be a real problem.

I usually avoid pointers at all costs and only ever use static for class members, so I'm not 100% sure what I'm doing. Is this guaranteed to work? Is there a better way?

[The context of this question is printing a complicated object in python, using the __str__ method. I define the method in my c++ code, which is then wrapped by SWIG. The SWIG example shows the use of static, but it's not clear to me that that's the only way. I am open to suggestions.]

最满意答案

正如@Prætorian所说,SWIG可以将std :: string返回给Python。 以下是我认为您正在研究的S​​WIG示例的一个示例。 还显示了一种避免在C ++中使用保留名称的方法:

%module x %{ #include "x.h" %} %include <windows.i> %include <std_string.i> %rename(__str__) display; %include "x.h"

XH

#include <sstream> #include <string> class Vector { public: double x,y,z; Vector(double a,double b,double c):x(a),y(b),z(c) {} ~Vector() {} #ifdef SWIG %extend { std::string display() { std::ostringstream temp; temp << '[' << $self->x << ',' << $self->y << ',' << $self->z << ']'; return temp.str(); } } #endif };

产量

Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import x >>> v = x.Vector(1.0,2.5,3.0) >>> print v [1,2.5,3]

As @Prætorian said, SWIG can return std::string to Python. Here's an example from the SWIG example I think you are looking at. Also shown is a way to avoid using a reserved name in C++:

x.i

%module x %{ #include "x.h" %} %include <windows.i> %include <std_string.i> %rename(__str__) display; %include "x.h"

x.h

#include <sstream> #include <string> class Vector { public: double x,y,z; Vector(double a,double b,double c):x(a),y(b),z(c) {} ~Vector() {} #ifdef SWIG %extend { std::string display() { std::ostringstream temp; temp << '[' << $self->x << ',' << $self->y << ',' << $self->z << ']'; return temp.str(); } } #endif };

Output

Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import x >>> v = x.Vector(1.0,2.5,3.0) >>> print v [1,2.5,3]

更多推荐

本文发布于:2023-08-07 21:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465594.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:char   const   Returning   static   ugly

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!