C++, Need Reason for error : cannot convert parameter 1 from 'char *' to 'const char *&'

后端 未结 3 2066
天涯浪人
天涯浪人 2020-12-20 13:04

Whey we cannot Convert pointer to a character ->TO-> a reference to a pointer to a constant character

I am interested in knowing the reason of syntax error when we

3条回答
  •  不思量自难忘°
    2020-12-20 13:19

    Suppose you had

    void foo_ptr(const char * & ptr)
    {         
        ptr = "readonlystring";
    }        
    

    You now call it as

        char *ptr;
        foo_ptr(ptr);
        *ptr = 0;
    

    Suppose no error were raised. You are writing to a read-only string, violating the type system without any casts.

    This is basically the CV version of How come a pointer to a derived class cannot be passed to a function expecting a reference to a pointer to the base class?.

提交回复
热议问题