Похожие чаты

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 ответов

22 просмотра

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

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

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

Господа, а что сейчас вообще с рынком труда на делфи происходит? Какова ситуация?
Rꙮman Yankꙮvsky
29
А вообще, что может смущать в самой Julia - бы сказал, что нет единого стандартного подхода по многим моментам, поэтому многое выглядит как "хаки" и произвол. Короче говоря, с...
Viktor G.
2
@Benzenoid can you tell me the easiest, and safest way to bu.y HEX now?
Živa Žena
20
This is a question from my wife who make a fortune with memes 😂😂 About the Migration and Tokens: 1. How will the old tokens be migrated to the new $LGCYX network? What is th...
🍿 °anton°
2
30500 за редактор? )
Владимир
47
а через ESC-код ?
Alexey Kulakov
29
What is the Dex situation? Agora team started with the Pnetwork for their dex which helped them both with integration. It’s completed but as you can see from the Pnetwork ann...
Ben
1
Гайс, вопрос для разносторонее развитых: читаю стрим с юарта, нада выделять с него фреймы с определенной структурой, если ли чо готовое, или долбаться с ринг буффером? нада у...
Vitaly
9
Anyone knows where there are some instructions or discort about failed bridge transactions ?
Jochem
21
@lozuk how do I get my phex copies of my ehex from a atomic wallet, to move to my rabby?
Justfrontin 👀
11
Карта сайта