c++ - invalid conversion from const char * to char * in Linux with Ternary Operator -


example 1:

char* message = ( m_message != null ? m_message : "" ); 

above line gives compiler error invalid conversion const char* char*. in g++ compiler. below piece of code works.

example 2:

char* message; if(m_message)     message = m_message; else     message = ""; 

why compiler error g++ in first example not second? both ways work fine in windows.

the string literal -> char* conversion allowed (but deprecated) if direct - can't let value "pass through" somewhere else decay const char*.

your second code conversion directly string literal char*, "only" deprecated.

the first 1 first pointer decay const char* - result of m_message != null ? m_message : "" const char* - , attempt convert resulting const char* char*, isn't allowed.

this code produces error, same reason:

const char* empty() { return ""; }  char* message; if(m_message)     message = m_message; else     message = empty(); 

Comments