以下是我的程序的两个变体
struct customer{
char fullname[35];
double payment;
};
int main()
{
customer alex{"Alex", 15};
return 0;
}
Run Code Online (Sandbox Code Playgroud)
struct customer{
char fullname[35];
double payment;
};
int main()
{
customer* alex = new customer {"Alex", 15};
return 0;
}
Run Code Online (Sandbox Code Playgroud)
第一个工作正常,但第二个引发error:could not convert '{"Alex", 15}' from 'brace-enclosed initializer list' to 'customer'. 有什么问题?
我在书中有下一个例子
const int** pp2;
int* p1;
const int n = 13;
pp2 = &p1; // not allowed, but suppose it were
*pp2 = &n; // valid, both const, but sets p1 to point at n
*p1 = 10; // valid, but changes const n
Run Code Online (Sandbox Code Playgroud)
但是,如果 pp2 是指向常量的指针,那么表达式 *pp2=&n 如何有效?