我会尽量避免在C++代码分配完全.也就是说,我只使用初始化并const尽可能地声明局部变量(即总是除了循环变量或累加器之外).
现在,我发现了一个不起作用的情况.我认为这是一般模式,但特别是在以下情况下出现:
假设我有一个程序将输入文件的内容加载到字符串中.您可以通过提供文件名(tool filename)或使用标准输入流(cat filename | tool)来调用该工具.现在,我如何初始化字符串?
以下不起作用:
bool const use_stdin = argc == 1;
std::string const input = slurp(use_stdin ? static_cast<std::istream&>(std::cin)
: std::ifstream(argv[1]));
Run Code Online (Sandbox Code Playgroud)
为什么这不起作用?因为原型slurp需要看起来如下:
std::string slurp(std::istream&);
Run Code Online (Sandbox Code Playgroud)
也就是说,我的论证是非 - const因此我不能将它绑定到临时的.似乎没有办法使用单独的变量.
目前,我使用以下解决方案:
std::string input;
if (use_stdin)
input = slurp(std::cin);
else {
std::ifstream in(argv[1]);
input = slurp(in);
}
Run Code Online (Sandbox Code Playgroud)
但这是以错误的方式揉搓我.首先,它是更多的代码(在SLOC中),但它也使用一个if而不是(这里)更逻辑的条件表达式,并且它在声明后使用我想要避免的赋值.
是否有一种避免这种间接初始化方式的好方法?这个问题可能会推广到需要改变临时对象的所有情况.难道流不是设计得不好以应对这种情况(const流没有意义,但是在临时流上工作确实有意义)?
可能重复:
C++ STL集更新很繁琐:我无法更改元素
我已经解决了问题并更改了名称,因此为了简单起见.
基本上我实例化一个类,并将其存储在std :: set中,稍后我想要引用该类,以便我可以检查它的值并修改它们......
简化代码:
MyClass tmpClass;
std::set<MyClass > setMyClass;
setMyClass.insert(tmpClass);
std::set<MyClass >::iterator iter;
iter=setMyClass.begin();
MyClass &tmpClass2=*iter;
Run Code Online (Sandbox Code Playgroud)
和错误:
error C2440: 'initializing' : cannot convert from 'const MyClass' to 'MyClass &'
Run Code Online (Sandbox Code Playgroud)
(我删除了部分错误消息"MVB :: Run ::"以清除它.)
如果我在最后一行代码中添加一个前面的"const",那么一切都运行良好但是我无法更改值...
这是正常行为,我必须删除数据,更改值并重新将其重新放回?
我觉得这与集合的排序有关,但我不会触及用于排序的变量.
可能重复:
如何初始化具有默认值的类的函数参数
#include <string>
void foo1(const std::string& s = std::string());
void foo2(std::string& s = std::string());
void foo3(const std::string s = std::string());
void foo4(std::string s = std::string());
Run Code Online (Sandbox Code Playgroud)
error at foo2(): default argument for ‘std::string& s’ has type ‘std::string {aka std::basic_string<char>}’
我理解编译器的观点,但我不知道这也不适用于foo1()它.
在C++中,std::pair<const T, const U>和之间的行为有什么不同const std::pair<T, U>?
我有一个长字节数组,我需要在我的C#代码中声明.我做这样的事情:
public static class Definitions
{
public const byte[] gLongByteArray = new byte[] {
1, 2, 3,
//and so on
};
}
Run Code Online (Sandbox Code Playgroud)
但是我得到一个错误,即const数组只能用空值初始化.
如果我改变const到static它编译,但我的问题是这样的-当我宣布它public static byte[] gLongByteArray不会在每次我的应用程序加载时初始化,对不对?在这种情况下,gLongByteArray变量将简单地指向在已加载到内存中的已编译exe/dll文件中定义的数组.我问的原因是因为这个数组很长,而且我不希望我的程序在每次应用程序启动时浪费CPU周期加载它,或者更糟糕的是,这个类被引用...
常量表达式是一个可以在编译时完全计算的表达式.因此,引用类型常量的唯一可能值是string和null引用.
根据:typeof(T)与Object.GetType()的性能,typeof(T)是一个编译时表达式.
那么为什么不能Type成为一个恒定的价值呢?
以下代码将无法编译:
public const Type INT_TYPE = typeof(int);
Run Code Online (Sandbox Code Playgroud) 好的,所以我在C++中有点像菜鸟,在我的第二次任务中,我需要使用公共和私有参数等来制作类.基本上,mutator函数不起作用,因为显然它们不是const类型的?
这是带有类的头文件:
class Customer {
private:
string PhoneNumber_;
string Name_;
string Address_;
public:
string get_PhoneNumber() const {return PhoneNumber_;} // Accessor
const void set_PhoneNumber(unsigned x) {PhoneNumber_ = x;} // Mutator
string get_Name() const {return Name_;}
const void set_Name(unsigned x) {Name_ = x;}
string get_Address() const {return Address_;}
const void set_Address(unsigned x) {Address_ = x;}
};
// declare the CreateCustomer function prototype with default values
Customer* CreateCustomer(const string& id = BLANK, const string& name = BLANK, const string& address = BLANK);
Customer* …Run Code Online (Sandbox Code Playgroud) 在c中,可以使用像这样的指针来改变const:
//mainc.c
#include <stdio.h>
int main(int argc, char** argv) {
const int i = 5;
const int *cpi = &i;
printf(" 5:\n");
printf("%d\n", &i);
printf("%d\n", i);
printf("%d\n", cpi);
printf("%d\n", *cpi);
*((int*)cpi) = 8;
printf(" 8?:\n");
printf("%d\n", &i);
printf("%d\n", i);
printf("%d\n", cpi);
printf("%d\n", *cpi);
}
Run Code Online (Sandbox Code Playgroud)
如果我们在c ++中尝试相同:
//main.cpp
#include <iostream>
using std::cout;
using std::endl;
int main(int argc, char** argv) {
const int i = 5;
const int *cpi = &i;
cout << " 5:" << '\n';
cout << &i << …Run Code Online (Sandbox Code Playgroud) 当实现一个接受指针参数包的函数时Ts...,为什么我const不能对指针进行限定,正如常规参数一样?
我在最新的GCC和Clang上遇到了不匹配的签名错误,我不明白为什么,因为指针const只是一个实现细节(因此它对于常规参数是合法的).
template<typename... Ts>
class C
{
void f(int*);
void g(Ts*...);
};
template<typename... Ts>
void C<Ts...>::f(int* const) {} // Legal
template<typename... Ts>
void C<Ts...>::g(Ts* const...) {} // Compiler error
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
prog.cc:12:16: error: out-of-line definition of 'g' does not match any declaration in 'C<Ts...>'
void C<Ts...>::g(Ts* const...) {}
^
1 error generated.
Run Code Online (Sandbox Code Playgroud)
您还可以在此处查看代码和错误.
我现在正在我的大学学习C ++和OOP的基础知识。我不确定100%分配函数给函数指针时如何工作。我遇到了以下代码:
void mystery7(int a, const double b) { cout << "mystery7" << endl; }
const int mystery8(int a, double b) { cout << "mystery8" << endl; }
int main() {
void(*p1)(int, double) = mystery7; /* No error! */
void(*p2)(int, const double) = mystery7;
const int(*p3)(int, double) = mystery8;
const int(*p4)(const int, double) = mystery8; /* No error! */
}
Run Code Online (Sandbox Code Playgroud)
从我的理解来看,p2和p3分配很好,因为函数参数类型匹配并且const-ness是正确的。但是,为什么p1和p4分配失败?将const double / int与非const double / int匹配是否不合法?