0

I came across some code. Right after the construction, the author casts the pointer into a void type. What is the purpose of this line? Is it like assert( m_some_class_ptr != 0) to check the integrity of the pointer?

Some_class * m_some_class_ptr = new Some_class();

(void)m_some_class_ptr;

Thank you.

2
  • 1
    can't be sure without seeing more code, but that's usually done to get rid of the "variable set but not used" warning from the compiler. Commented Mar 19, 2014 at 23:32
  • you know what, i think you are right. These pointers are all global variables (evil, i know), which are not used in the context. Commented Mar 19, 2014 at 23:35

1 Answer 1

1

Cast to void is a common idiom to avoid compiler warnings that a variable is unused. If the initialisation was the only use of m_some_class_ptr, you would quite possible get a warning. So you want to add a second use of the variable, but without doing anything.

(void)m_some_class_ptr; does nothing. It evaluates the expression m_some_class_ptr and throws the result away.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.