C++ 常量转换

标签 c++ casting constants

我正在尝试打印 const 的值,但它不起作用。多年后我正在回归 C++,所以我知道转换是一个可能的解决方案,但我也无法让它工作。

代码如下:

  //the number of blanks surrounding the greeting
    const int pad = 0;

    //the number of rows and columns to write
    const int rows = pad * 2 + 3;
    const string::size_type cols    =       greeting.size() + pad * 2 + 2;

    cout << endl << "Rows : " + rows;

我试图打印“行”的值但没有成功。

最佳答案

你想要:

cout << endl << "Rows : " << rows;

请注意,这与 const 无关 - C++ 不允许您使用 + 运算符连接字符串和数字。你实际上在做的是那个神秘的东西,叫做指针运算。

关于C++ 常量转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6256197/

相关文章:

c# - IsAssignableFrom 与 COM

c++ - 如何从 C++ 代码访问 .qrc 文件中指定的文件?

c++ - QSortFilterProxyModel 对多列进行排序

java - 使用 Hibernate Query.list() 避免类型安全警告

ios - UIKeyboardFrameBeginUserInfoKey

c++ - "const"有什么用?

perl - 如何在 Perl 的单独文件中定义常量?

c++ - 更改生成文件版本

c++ - RocksDB 中的可移植二进制文件

c++ - 多重继承 C++,转换是如何工作的?