在示例代码中,调用message()永远不会影响类的内容,所以我希望方法是const.但是我也不希望返回值const,所以const_cast下面使用它是否安全?还是有更好的解决方案?
编辑:抱歉..这n_是一个指针!
编辑2:我终于设法正确地重新产生了问题.如果n_是一个数组int n[100]怎么办?然后我看到没有问题const_cast.
class A
{
private:
int n_[10];
public:
/* ... */
int* n() const
{
return const_cast<int*>(n_);
}
};
Run Code Online (Sandbox Code Playgroud) #include <stdio.h>
int main(){
const int a = 10;
*(int*)(&a) = 9; // modify a
printf("%d", a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么?
我是C++的新手,我正在尝试修改一些现有的代码.我基本上必须在C++中修改const引用变量.有办法吗?
我想从常量字符串引用中删除子字符串.这显然不起作用,因为id是一个常量引用.修改id的正确方法是什么?谢谢.
const std::string& id = some_reader->Key();
int start_index = id.find("something");
id.erase(start_index, 3);
Run Code Online (Sandbox Code Playgroud) #include <stdio.h>
#include <stdlib.h>
#define MAX 15 //line that give problems
int linearSearch(int v[], int MAX, int valore);
int main()
{
int ris, valore, v[]={1,1,1,1,1,1,1,1,1,12,1,1,1,1,1};
scanf("%d", &valore);
ris = linearSearch(v, MAX, valore);
printf("%d", ris);
return 0;
}
int linearSearch(int v[], int MAX, int valore)
{
int i;
for (i=0;i<MAX;i++)
{
if(valore==v[i])
return i;
}
return -1;
}
Run Code Online (Sandbox Code Playgroud)
为什么这段代码在编译时报告错误?如果我用预处理器指令替换它,为什么它运行正常
const int MAX = 15;
Run Code Online (Sandbox Code Playgroud) 我试图逐行读取文件.将每一行转换为以空字符结尾的字符串.将所有行推入向量并返回.
vector<const char*> TypeGLscene::LoadGLshader (string ThisFile)
{
ifstream fromFile;
fromFile.open(ThisFile);
if (fromFile.good()==false)
{
cout<<"Could not find file: "<<ThisFile<<endl;
exit(1);
}
vector<const char*> FileContents;
const char* OneLine;
string LineStr;
while (fromFile.good()==true)
{
getline(fromFile,LineStr);
OneLine = LineStr.c_str();
FileContents.push_back(OneLine);
}
fromFile.close();
return FileContents;
Run Code Online (Sandbox Code Playgroud)
问题是所有char字符串都是在堆栈中创建的,函数返回char*的向量.
我试图通过以下方式在堆上分配内存:
OneLine = new char[LineStr.size()+1];
Run Code Online (Sandbox Code Playgroud)
但我被卡住了,因为一旦分配,我就无法复制任何内容; 内容是const.
我怎么能在const char*上使用new关键字并在它实现它是const之前同时向它添加内容?
(更不用说我必须一个一个地删除它们,另一方面......真是一团糟)
编辑:我宁愿返回一个向量,但这一切都是因为我不知道快速(一行)方式将向量转换为const char**:
void glShaderSource(GLuint shader,
GLsizei count,
const GLchar **string,
const GLint *length);
Run Code Online (Sandbox Code Playgroud) 有人可以向我解释C++中const iterator引用和non-const iterator引用之间的区别吗?为什么我不能non-const iterator在print函数中使用引用?
以下代码无法编译.
#include <deque>
#include <iostream>
using namespace std;
template<typename T> ostream & print(T & start, T & end) {
for (; start != end; ++start) {
cout << *start << " ";
}
return cout;
}
int main() {
int tab[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
deque<int> d1(tab, tab + 10);
deque<int> d2;
deque<int>::iterator it;
for (it = d1.begin(); it != d1.end(); …Run Code Online (Sandbox Code Playgroud) 我目前正在开发一个项目,包括使用void指针的一些通用的链表实现.为这些列表提供了一些非常好的函数,我决定只使用元素的识别函数(const void*).在添加const关键字之后是必要的,我想到了我的代码现在是多么正确(如果我实现了之前的所有内容).
由于编译器(GCC)没有警告我,我决定接受测试.我使用"gcc -g -Wall test.c"编译了以下代码,并且没有收到GCC的任何警告.
#include <stdio.h>
#include <stdlib.h>
void testf(const void *testp){
*((int32_t *) testp) += 1;
}
void testf2(const int32_t *testp){
*((int32_t *) testp) += 1;
}
int main(){
int32_t testv = 0;
printf("%i \n", testv);
testf(&testv);
printf("%i \n", testv);
testf2(&testv);
printf("%i \n", testv);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出如下:
0
1
2
Run Code Online (Sandbox Code Playgroud)
我没想到C会因此而崩溃,但我希望编译器收到警告.在这个例子中我只是强制转换,在我的实际函数中,我也将const void指针赋给tmp变量.
这是一个错误吗?
鉴于今天的编译器有多复杂,我至少期待一个警告,即我正在向非const指针投射指针.如果我更改了强制转换并在那里添加了const关键字,GCC会抛出我尝试分配给只读位置的常见错误
我是否应该重新考虑我对表示const指针的函数的信任?这不是我所理解的合同:)
场景1:当const变量在内部声明时main(),即变为局部变量
#include <stdio.h>
#include <conio.h>
main()
{
const int a = 45;
* ((int*)&a)=50;
printf("%d\n",a);
getch();
}
Run Code Online (Sandbox Code Playgroud)
输出MVC++:
一个= 50
场景2:当const变量在外部声明时main(),即变为全局变量
#include <stdio.h>
#include <conio.h>
const int a = 45;
main()
{
* ((int*)&a)=50;
printf("%d\n",a);
getch();
}
Run Code Online (Sandbox Code Playgroud)
输出MVC++:
未处理的异常......违规行为.
我理解为什么在尝试修改全局定义的const变量时会出现错误.但是,当我尝试修改const本地定义的变量时,为什么我没有收到错误?
假设我有一个指向整数的指针数组(即每个元素都是指向int的指针)
int** ptrArray;
Run Code Online (Sandbox Code Playgroud)
我想阻止更改数组条目指向的整数
我需要把const放在哪里?
1. const int ** ptrArray
2. int const ** ptrArray
3. int *const* ptrArray
4. int ** const ptrArray
Run Code Online (Sandbox Code Playgroud)
对此有什么规定吗?像"第一个const保护数据","第二个const保护指针"等等?
const的位置背后是否有任何逻辑?放置的地方与保护的地方之间有什么联系?
这对我来说是一个令人困惑的问题,如果有人可以给我任何指导或链接,我会真的很高兴我可以阅读更多关于如何以及在何处使用const基于我想要保护的内容(如果我需要使用三维数组中的const或左右)
以下讨论:
我对JavaScript中的函数声明有疑问.
通过匿名函数声明我的意思是这样的(https://en.wikibooks.org/wiki/JavaScript/Anonymous_Functions):
var myFunction = function (fruit){
alert('I like ' + fruit);
}
Run Code Online (Sandbox Code Playgroud)
而const我的意思是:
const myfunction = (fruit) => alert('I like ' fruit);
Run Code Online (Sandbox Code Playgroud)
使用匿名函数或使用const更快吗?我已经读过使用const允许在JavaScript中进行编译优化.有什么理由我应该使用一个而不是另一个?
这甚至是相关的吗?