How to pass an std::string to glShaderSource?

后端 未结 7 1842
感情败类
感情败类 2020-12-05 04:47

I have the following code:

glShaderSource(shader, 1, (const char **)data.c_str(), NULL);

But it makes my program crash. How do I convert

7条回答
  •  南笙
    南笙 (楼主)
    2020-12-05 05:14

    You can get a reasonable-looking call by using a helper class. Define this class:

    struct StringHelper {
      const char *p;
      StringHelper(const std::string& s) : p(s.c_str()) {}
      operator const char**() { return &p; }
    };
    

    Then, when you need to call glShaderSource, do it this way:

    glShaderSource(shader, 1, StringHelper(data), NULL);
    

提交回复
热议问题