Skip to main content
GameDev.net gamedev.net
🔒 Locked

const arguments

Started by jonbell Jan 1, 2004 at 9:01 AM 2 replies 350+ views
Original Post
jonbell
jonbell
Hi I want to pass a const void ptr argument to a function and take a copy of it like so : void myclass::myfunc ( const void *buffer ) { m_buffer = buffer; } where m_buffer is a normal void* (not const) I get an error telling me that a void* and const void* are incompatible. I know its a good practice to const your arguments that will not be changed so what do i need to do? I know i could cast it but that seems to defeat the purpose of all of this. Ta.
sBibi
sBibi
quote:
I know its a good practice to const your arguments that will not be changed


it''s not "will not be changed", it''d rather be "cannot be changed", or must not. by declaring something const, think of it as making it read-only.
Cedric
Cedric
You get an error because the compiler wants to prevent you to modify the stuff pointed to by buffer, using m_buffer.

Why don''t you make m_buffer constant? That should (IIRC) fix your problem. If m_buffer is not meant to be constant, then your parameter to myfunc probably shouldn''t be constant too.

Cédric

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.