const-pointer

C++ Best way to have class member which is a pointer (or reference) to another class and can handle both const and non-const situations

不想你离开。 提交于 2021-02-11 12:42:48
问题 Consider the following sample code. It compiles and works as expected. However, if I add "const" to the beginning of the first line of main function, it will not compile, because B class takes a pointer to A , and with const being added, we will have a pointer to const A instead. Templatizing B is an option, but I wonder if there are cleaner/nicer solutions for this. I am open to various suggestions, including moving the B inside A if that would help (B is used by A only). #include <vector>

Dynamically allocating memory for const char string using malloc()

[亡魂溺海] 提交于 2020-01-13 11:07:06
问题 I am writing a program that reads a value from an .ini file, then passes the value into a function that accepts a PCSTR (i.e. const char *). The function is getaddrinfo() . So, I want to write PCSTR ReadFromIni() . To return a constant string, I plan on allocating memory using malloc() and casting the memory to a constant string. I will be able to get the exact number of characters that were read from the .ini file. Is that technique okay? I don't really know what else to do. The following

Iterating through C-style array not using a pointer

人走茶凉 提交于 2020-01-02 05:44:08
问题 I am learning pointer arithmetic and have a piece of code giving me error for quite some time. any help would be appreciated.(I couldnt find it on SO) int arr [] = {1, 2, 3, 4, 5} ; for (int i = 0 ; i < 5 ; i++) { cout << *arr ; arr++ ; } cout << *arr << endl ; I am unable to understand the error i am getting in codeblocks. I am getting this statement. error: lvalue required as increment operand| ||=== Build finished: 1 errors, 0 warnings ===| In this code i have to iterate the array without

new-expression and delete-expression on const reference and const pointer

怎甘沉沦 提交于 2019-12-11 22:19:50
问题 C++ Much literature says const references cannot be used to modify their referents and const pointers cannot be used to modify their pointees. Then, why can they be delete d? const int& cirDynamic = *( new int(5) ); // ^ 'const int& cirDynamic = *( &( *( new int(5) ) ) );' gives same output below cout << cirDynamic << endl; // 5 delete &cirDynamic; cout << cirDynamic << endl; // garbage value I know the trailing const in T* const only prevents the pointer from being reseated, but below I use

Prefer Iterators Over Pointers?

一曲冷凌霜 提交于 2019-12-10 19:55:58
问题 This question is a bump of a question that had a comment here but was deleted as part of the bump. For those of you who can't see deleted posts, the comment was on my use of const char* s instead of string::const_iterator s in this answer: "Iterators may have been a better path from the get go, since it appears that is exactly how your pointers seems be treated." So my question is this, do iterators hold string::const_iterator s hold any intrinsic value over a const char* s such that

Iterating through C-style array not using a pointer

岁酱吖の 提交于 2019-12-05 16:11:29
I am learning pointer arithmetic and have a piece of code giving me error for quite some time. any help would be appreciated.(I couldnt find it on SO) int arr [] = {1, 2, 3, 4, 5} ; for (int i = 0 ; i < 5 ; i++) { cout << *arr ; arr++ ; } cout << *arr << endl ; I am unable to understand the error i am getting in codeblocks. I am getting this statement. error: lvalue required as increment operand| ||=== Build finished: 1 errors, 0 warnings ===| In this code i have to iterate the array without dereferencing or using [] operator. Coding Mash You are getting this error as you trying to increment

Dynamically allocating memory for const char string using malloc()

半世苍凉 提交于 2019-12-05 15:21:20
I am writing a program that reads a value from an .ini file, then passes the value into a function that accepts a PCSTR (i.e. const char *). The function is getaddrinfo() . So, I want to write PCSTR ReadFromIni() . To return a constant string, I plan on allocating memory using malloc() and casting the memory to a constant string. I will be able to get the exact number of characters that were read from the .ini file. Is that technique okay? I don't really know what else to do. The following example runs fine in Visual Studio 2013, and prints out "hello" as desired. const char * m() { char * c =

c - what does this 2 const mean?

China☆狼群 提交于 2019-11-29 12:34:40
code: const char * const key; There are 2 const in above pointer, I saw things like this the first time. I know the first const makes the value pointed by the pointer immutable, but did the second const make the pointer itself immutable? Anyone can help to explain this? @Update: And I wrote a program that proved the answer is correct. #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