它可能听起来很愚蠢.在C++ prime第5版P258中,它说:
默认情况下,它的类型是指向类类型的非对象版本的const指针.例如,默认情况下,Sales_data成员函数中的类型是Sales_data*const.
我可以理解,对于这个*是一个const指针,这意味着它初始化后指向的对象不能改变.但是它说:
虽然这是隐式的,但它遵循正常的初始化规则,这意味着(默认情况下)我们不能将它绑定到const对象.
但我写了以下代码,它仍然编译好:
class Test{
public:
Test() = default;
Test(const string &s): teststr(" ") {};
Test(int a) : testint(a) {};
Test(const string &s, int a): teststr(s), testint(a) {};
string getstr() const { return teststr; };
int getint() { return testint; }; //there is no const here
private:
string teststr;
int testint = 0;
};
int main(){
Test a("abc",2);
cout << a.getint() << " ";
cout << a.getstr() << endl;
cout << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:如果编译器能够很好地编译它是否存在'const',为什么它重要?然后这本书说:
毕竟,isbn的主体不会改变这个指向的对象,所以如果这是一个指向const的指针,我们的函数会更灵活. …
以下 C++ 代码无法编译,因为它将非常量指针传递给需要 const 指针的find()函数。
#include <map>
std::map<int*, double> mymap;
double myfind(const int * mykey)
{
return mymap.find(mykey)->second;
}
Run Code Online (Sandbox Code Playgroud)
有没有办法在不改变地图类型或使变量mykey非常量的情况下使查找工作?毕竟函数find()不会修改指向的对象,它只是比较指针。
我有这个函数签名:
void myFunction(int *const ptr);
Run Code Online (Sandbox Code Playgroud)
什么是点const的关键字在这个特定背景下?
即使ptr不是const变量,我也无法修改它指向的地址,因为它是按值传递的,所以如果我有另一个这样的函数:
void myAnotherFunction(int *ptr);
Run Code Online (Sandbox Code Playgroud)
在它的实现中做了这样的事情:
//...
ptr = malloc(1023);
//...
Run Code Online (Sandbox Code Playgroud)
这不会影响传递的值(当然在这个函数之外)。
所以问题是:使用myFunction()签名而不是签名myAnotherFunction()有什么意义?(除此之外,您还会收到编译时错误)。
通常,C 文件 I/O 是使用完成的,FILE*因为这是标准库函数都采用的。
在 C 中,也可以有一个const 指针,指针的地址不能改变(但它指向的值可以)。
我想知道这是否可以应用于FILE*,所以写了这个小程序来测试这个:
#include <stdio.h>
int main(void) {
FILE* const file = fopen("somefile.txt", "w");
if (file != NULL) {
fprintf(file, "%s\n", "So this works? That's just swell!");
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这可以很好地编译并使用 GCC 在 Ubuntu/Linux 16.04 上按预期工作(该文件包含我预期的字符串),但我不确定这个习语是否是一个好主意 - 该FILE类型在设计和处理上是不透明的是特定于实现的。
因为不能保证任何 C 库实现都不会尝试更改FILE指针的地址,所以在 C 中执行 I/O 时不使用这种方法是否更安全?
我可以使指针指向字符串文字,它将字符串文字转换为常量指针,但对指针的引用不能分配字符串文字,例如:
const char *&text = "Hello, World\n";
Run Code Online (Sandbox Code Playgroud)
有一个错误,编译器说我不能将数组转换为指针引用,我很好奇,为什么它不正确?
我正在学习指针算术并且有一段代码在很长一段时间内给我错误.任何帮助将不胜感激.(我无法在SO上找到它)
int arr [] = {1, 2, 3, 4, 5} ;
for (int i = 0 ; i < 5 ; i++)
{
cout << *arr ;
arr++ ;
}
cout << *arr << endl ;
Run Code Online (Sandbox Code Playgroud)
我无法理解我在codeblocks中遇到的错误.我收到这个声明.
error: lvalue required as increment operand|
||=== Build finished: 1 errors, 0 warnings ===|
Run Code Online (Sandbox Code Playgroud)
在此代码中,我必须迭代数组,而无需解除引用或使用 [] 运算符.
我正在编写一个从.ini文件中读取值的程序,然后将该值传递给接受PCSTR的函数(即const char*).功能是getaddrinfo().
所以,我想写PCSTR ReadFromIni().要返回一个常量字符串,我计划使用malloc()内存分配内存并将内存转换为常量字符串.我将能够获得从.ini文件中读取的确切字符数.
这种技术还好吗?我真的不知道还能做什么.
以下示例在Visual Studio 2013中正常运行,并根据需要打印出"hello".
const char * m()
{
char * c = (char *)malloc(6 * sizeof(char));
c = "hello";
return (const char *)c;
}
int main(int argc, char * argv[])
{
const char * d = m();
std::cout << d; // use PCSTR
}
Run Code Online (Sandbox Code Playgroud) 我想使一个常量双指针指向一个指向常量double的常量指针.我从头开始制作它(当然我在书上做了一些搜索,然后我用Google搜索)并思考以下三个:
const double* cp; //pointer to a constant double
double *const cp; //constant pointer
const double *const cp; //constant pointer to a constant double
Run Code Online (Sandbox Code Playgroud)
我认为下一步是编写一个常量双指针
double **const cp;// double constant pointer
Run Code Online (Sandbox Code Playgroud)
然后我结合最后两个语句和我写
const double *const cp = arr[0];
double **const cp1 = arr ;
Run Code Online (Sandbox Code Playgroud)
其中arr是动态分配的双维数组.之后我试着验证我做了什么,并写下了以下语句,期望产生所有错误.
**cp1 = 1; // didn't produce error
*cp1 = arr[4]; // didn't produce error
cp1 = new double*[5]; //produce error
Run Code Online (Sandbox Code Playgroud)
所以问题是我无法完成上面描述的内容,一个常量双指针指向一个指向常量double的常量指针.我该怎么做?
提前致谢.
我想用变换转换CGPath,CGAffineTransformMakeRotation(radians)但CGPathCreateCopyByTransformingPathfunc需要一个CConstPointer<CGAffineTransform>.我该如何CConstPointer<CGAffineTransform>摆脱我的CGAffineTransform?
这很可能以前以其他方式被问过(如果不是,我会感到惊讶)但如果是,我正在努力寻找它。
鉴于:
#include <iostream>
#include <string>
int main()
{
int * const pi = new int(1);
long int * const pl = reinterpret_cast<long int * const>(pi);
std::cout << "val: " << *pl << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我收到警告:
<source>: In function 'int main()':
<source>:7:27: warning: type qualifiers ignored on cast result type [-Wignored-qualifiers]
7 | long int * const pl = reinterpret_cast<long int * const>(pi);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ASM generation compiler returned: 0
<source>: In function 'int main()':
<source>:7:27: warning: type …Run Code Online (Sandbox Code Playgroud) 码:
const char * const key;
Run Code Online (Sandbox Code Playgroud)
上面的指针有2个const,我第一次看到这样的东西.
我知道第一个const使指针指向的值不可变,但第二个const是否使指针本身不可变?
有人可以帮忙解释一下吗?
@Update:
我写了一个程序,证明答案是正确的.
#include <stdio.h>
void testNoConstPoiner() {
int i = 10;
int *pi = &i;
(*pi)++;
printf("%d\n", i);
}
void testPreConstPoinerChangePointedValue() {
int i = 10;
const int *pi = &i;
// this line will compile error
// (*pi)++;
printf("%d\n", *pi);
}
void testPreConstPoinerChangePointer() {
int i = 10;
int j = 20;
const int *pi = &i;
pi = &j;
printf("%d\n", *pi);
}
void testAfterConstPoinerChangePointedValue() {
int i = 10; …Run Code Online (Sandbox Code Playgroud) 据我所知,const int *暗示我可以改变指针而不是数据,int * const说我不能改变指针地址,但我可以改变数据,并const int * const声明我不能改变它们中的任何一个.
但是,我无法更改使用该类型定义的指针的地址const int *.这是我的示例代码:
void Func(const int * pInt)
{
static int Int = 0;
pInt = ∬
Int++;
}
int wmain(int argc, wchar_t *argv[])
{
int Dummy = 0;
const int * pInt = &Dummy;
//const int * pInt = nullptr; // Gives error when I try to pass it to Func().
std::cout << pInt << '\t' << *pInt << std::endl;
std::cout << "-------------------" …Run Code Online (Sandbox Code Playgroud) const-pointer ×13
c++ ×10
pointers ×5
c ×4
const ×2
arrays ×1
casting ×1
char-pointer ×1
const-cast ×1
const-char ×1
constants ×1
file-io ×1
iterator ×1
malloc ×1
reference ×1
stl ×1
string ×1
swift ×1