Похожие чаты

I have a method that takes a const char* buf

and using this function i need to set a member variable which is a char* m_buf. c++ didn't like a plain = on them ("a value of type "const char *" cannot be assigned to an entity of type "char *"")
i don't know any other way to set it, though, everything that i've googled leads to a solution that is either unrelated to my problem or just can't be incorporated. is there a "non-const cast"? (note: i can't change the function signature, and i can't change the type of that member variable, since it must be non-const)

12 ответов

21 просмотр

const_cast ?

in c++ char* is deprecated because it has an undefined behavior eg: char* text = "text"; text[0] = 'a'; // undefined behavior you could use this if you just can't use anything else std::string s = "text"; char* c_text = const_cast<char*>(s.c_str()); std::cout << c_text << '\n';

Show function signature, variable definition and how you call the function.

artemetra 🇺🇦-[less active] Автор вопроса
Ender
Show function signature, variable definition and h...

void set( const char* buf, int size ) this function is a member of a Coder class inside that class: private: char* m_buf; // i can change this but this must not be a const variab;e! example of a function call: Coder coder; coder.set(buf_hello, size); buf_hello is a const char* cstring literal and i can't change it in any way (neither can i change how the function is called)

artemetra 🇺🇦 [less active]
void set( const char* buf, int size ) this functio...

Change the function itself so that it copies data (not the pointer!) from the argument.

artemetra 🇺🇦-[less active] Автор вопроса
Ender
Change the function itself so that it copies data ...

hi, i tried using std::copy like this std::copy(buf, buf + size, m_buf); but it results in a segmentation fault. i've never used copy before so i'm not sure what i'm doing wrong, buf points to the first character and buf + size points to the null terminator, so it should copy the data in that range to m_buf, right?

artemetra 🇺🇦-[less active] Автор вопроса
Ender
Did you allocate the momory for the copy?

m_buf is initialized, is there anythinп else i should do? in the examples i've seen for std::copy there weren't any mallocs or similar things..

artemetra 🇺🇦 [less active]
hi, i tried using std::copy like this std::copy(b...

std::copy has a bug in libstdc++ in some old versions of gcc

Похожие вопросы

Обсуждают сегодня

30500 за редактор? )
Владимир
47
а через ESC-код ?
Alexey Kulakov
29
Anyone knows where there are some instructions or discort about failed bridge transactions ?
Jochem
21
Also, why can’t the community have a vote/ say when it comes to initiatives like buybacks. Isn’t the point of crypto decentralisation? Don’t we deserve input as long term supp...
👨🏽‍🦰
13
Чёт не понял, я ж правильной функцией воспользовался чтобы вывести отладочную информацию? но что-то она не ловится
notme
18
Привет)) уже кажется эту тему перемусолили, но вот я так и не понял. Я сейчас сижу на 27дюймов 2к мониторе. На Актуальной макоси, если я куплю 27д 4к монитор: - будет ли изобр...
Vladislav Piskunov
16
any reference of this implementation?
BitBuddha
29
Hi guys, any problem with Pulsebrige? Trying to transfer from wETH to ETH. First it tells me to connect my metamask "through mobile app" not desktop. Then I did and confirmed ...
Snowflakecrypto
13
У меня есть функция где происходит это: write_bit(buffer, 1); write_bit(buffer, 0); write_bit(buffer, 1); write_bit(buffer, 1); write_bit(buffer, 1); w...
~
13
Страшнейшая правда про списки ЦБ. С первых дней жизни P2P сферы, молодые человеки, начитавшись законодательной базы и "внутренних" документов, решили, что им противостоит сер...
Foxcool
3
Карта сайта