How to pass an std::string to glShaderSource?(如何将 std::string 传递给 glShaderSource?)
问题描述
我有以下代码:
glShaderSource(shader, 1, (const char **)data.c_str(), NULL);
但它使我的程序崩溃.如何将 std::string 转换为 const char ** ?我也试过 (const char **)& 但它说需要 l 值",我不明白.当我使用此代码时它工作正常:
But it makes my program crash. How do I convert std::string into const char ** ?
I also tried (const char **)& but it said "requires l-value" which I don't understand. It works fine when I use this code:
const char *data = "some code";
glShaderSource(shader, 1, &data, NULL);
但我不能直接从 std::string 让它工作.我可以为它分配一个新的 char 数组,但这不是很好的代码.
But I can't make it work directly from a std::string. I could allocate a new char array for it but that is not nice code.
我也尝试过 const GLchar 但显然它没有区别.
I also tried with const GLchar but obviously it makes no difference.
推荐答案
data.c_str() 返回一个 const char*,所以这样做:
data.c_str() returns a const char*, so do this:
const char *c_str = data.c_str();
glShaderSource(shader, 1, &c_str, NULL);
这篇关于如何将 std::string 传递给 glShaderSource?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将 std::string 传递给 glShaderSource?
基础教程推荐
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- CString 到 char* 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
