Dereferencing a pointer to constant

前端 未结 10 688
深忆病人
深忆病人 2020-12-30 23:09

Let\'s say we have a class called object.

int main(){
    object a;
    const object* b = &a;
    (*b); 
}

Question: b is a pointer to

10条回答
  •  失恋的感觉
    2020-12-30 23:44

    The type of an expression is just based on the declared types of the variables in the expression, it can't depend on dynamic run-time data. For instance, you could write:

    object a1;
    const object a2;
    const object *b;
    if (rand() % 2 == 0) {
        b = &a1;
    } else {
        b = &a2;
    }
    (*b);
    

    The type of *b is determined at compile-time, it can't depend on what rand() returns when you run the program.

    Since b is declared to point to const object, that's what the type of *b is.

提交回复
热议问题