gpt4 book ai didi

c++ - 强制用户声明对象常量

转载 作者:太空狗 更新时间:2023-10-29 21:24:26 25 4
gpt4 key购买 nike

<分区>

有没有办法强制类的用户根据用于构造函数的数据构造const 对象?

例如,考虑围绕一些缓冲区的小型包装类,它可以是 const 或非 const

class Wrapper {
public:
Wrapper(const char*);
Wrapper(char*);
};

现在,如果用户提供一个 const,我可以强制他们在编译时将对象声明为 const。也就是

// you have to do this if the input is const 
const char* a;
// this will not compile
Wrapper w(a);
// but this will
const Wrapper(a);

有什么想法吗?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com