小编ale*_*lex的帖子

通过 new 运算符初始化结构的问题

以下是我的程序的两个变体

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'. 有什么问题?

c++ new-operator c++11

6
推荐指数
1
解决办法
75
查看次数

cpp中的指针

我在书中有下一个例子

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 如何有效?

c++ c++11

1
推荐指数
1
解决办法
68
查看次数

标签 统计

c++ ×2

c++11 ×2

new-operator ×1