Answer the question
In order to leave comments, you need to log in
Is it possible to initialize a const string not in a constructor?
A question.
I have a class with a string field (unsigned char* str). This field is not initialized in the constructor, but in one of the class methods. I would like to make a field (unsigned char * const) so that when calling its get-function, it would be possible to get a pointer to this string, but not give the opportunity to change the data that was written during initialization.
Is there any way to do this with little blood? If everything is left as is, then the compiler swears that there is no initialization at the declaration and in the constructor. If you add it to the constructor (str = NULL), then, accordingly, it will be impossible to change the data.
Plus, there is such a remark, initialization occurs as follows:
The method receives a pointer to TCHAR*, which is then converted to char*, which is explicitly cast to unsigned char*. Having received a pointer to this string (it is temporary and makes sense only within the scope of the initializer), str = new [*string length*] is called; and then memcpy copies one to the other. So there is a double assignment, first str = new, then memcpy(str,...). Is it possible to die right here with the dream of having constant data?
PS std::string is not used intentionally. But if there are no other options, replacement with it is possible, but it is desirable that the get method still returns unsigned char * const.
Thank you in advance.
Answer the question
In order to leave comments, you need to log in
The short answer to the question in the title is no, you can't. But if I correctly understood the task - to avoid the possibility of modification, then you can come up with several solutions - give a copy by value (std:: string is good here), take a pointer to the destination string as a parameter and copy it there, copy it to a constant buffer created by right there on the stack and give a pointer to it ...
const members, as well as members that are references, must be initialized in the constructor.
It is not entirely clear why a pointer should become constant if you want to prohibit changing the data written on it, and not itself. Either there is an inaccuracy in the description, or I wanted to say not unsigned char* const, but const unsigned char*. But regardless of this, the const attribute can be bypassed with the const_cast modifier (or a banal C-like type casting). The main thing at the same time is that the reducible memory does not end up in the read-only area, but if it is explicitly allocated by new, then such an incident, in theory, should not happen.
unsigned char* const is a constant pointer to a mutable char array
you need
const unsigned char * is a non-const pointer to an immutable char array
then you can change the value of the pointer inside the class (if your field is private), but no one will be able to change the data itself (without const_cast ).
proof: habrahabr.ru/post/100104/
>> So there is a double assignment, first str = new, then memcpy(str,...).
no double assignment occurs
str = new unsigned char[size];
only allocates memory and remembers where this memory is allocated, the memory itself is not initialized.
data copying happens inside memcpy.
To be fair, if you have typeof( str ) == typeof( used const char * ), then you can't call memcpy without a type cast, because you have a pointer to immutable data.
you need:
class A
{
private:
unsigned const char *str;
A(): str( nullptr ) // you can not initialize in principle, if you are sure
// that you will not read it before initialization
{
}
init(… )
{
unsigned char *ptr = new unsigned char[size];
memcpy( ptr,… );
str = ptr; // you don't change the data itself here, only the pointer to the data
}
used const char *get() const{ return str; }
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question