- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
只是想知道在传递 char* 时不使用 const_cast 并简单地将其类型转换为 (char *) 或两者基本相同是否有缺点?
#include <iostream>
#include<conio.h>
using namespace std;
void print(char * str)
{
cout << str << endl;
}
int main ()
{
const char * c = "sample text";
// print( const_cast<char *> (c) ); // This one is advantageous or the below one
print((char *) (c) ); // Does the above one and this are same?
getch();
return 0;
}
使用 print((char *) (c) );
有什么缺点吗?在 print( const_cast<char *> (c) );
还是基本上两者相同?
最佳答案
首先,您的 print
函数应该采用 const char*
参数,而不仅仅是 char*
,因为它不会修改它。这消除了对任何一个转换的需要。
至于您的问题,C++ 风格的转换(即 const_cast
、dynamic_cast
等)优于 C 风格的转换,因为它们表达了转换的意图并且他们很容易搜索。如果我不小心使用了 int
类型的变量而不是 const char*
,使用 const_cast
将导致编译时错误。但是,如果我使用 C 风格的转换,它会编译成功,但会在运行时产生一些难以诊断的内存问题。
关于c++ - 如果我不执行 const_cast<char*> 并仅使用 ( char * ) 类型转换,会有什么危害吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9283179/
我正在阅读 C++ Primer,我发现了一些非常奇怪且难以理解的东西: Record lookup(Account&); //Record and Account are two unrelate
我的问题是,为什么代码的第一部分不起作用而第二部分有效。非常量指针应该修改之前使用 const_cast 的 const 值,但是对于整数这个技巧不起作用。您能解释一下为什么会这样吗? const i
我已经阅读了很多关于 C++ 中的 const_cast 被认为是错误和危险的讨论,除了向后兼容 C 代码之外,不应将其用于任何其他用途。我大体上同意。 但是最近我遇到了以下用例,这让我感到好奇。 我
C++ Primer 一书的第 6.4 章陈述如下: In § 4.11.3 (p. 163) we noted that const_casts are more useful in the con
我看到这个 post这解释了const_cast<>并说使用指针/引用是有益的。但是,请考虑以下代码: 1- const_cast(xadj) 我得到 invalid const_cast from
我正在阅读有关 c++ 中的 const_cast 运算符 1.我无法理解的第一件奇怪的事情是 const_cast 运算符语法即 -const_cast----(--expression--)---
这个问题在这里已经有了答案: const_cast doesn't work c++? [duplicate] (3 个答案) 关闭 6 年前。 在const_cast之后,main函数中的值没有变
这个问题在这里已经有了答案: Two different values at the same memory address (7 个答案) 关闭 5 年前。 我有以下代码: int main(){
这个问题在这里已经有了答案: Two different values at the same memory address (7 个答案) 关闭 5 年前。 #include using nam
我想使用 C++ 格式进行此转换,它以 C 方式工作。但是当我尝试使用 C++ 格式时它失败了。 有效! void req_password(const void *data, size_t data
我有一个管理输入的类。要显示和更改键绑定(bind),重要的是在完成后将其提交给管理器之前,为调用者提供它可以拥有和更改的绑定(bind)的映射。但是,这个映射中可以插入/删除什么的具体规则只有管理者
我对 const_cast 的返回类型有点困惑?尖括号内的类型是否 <>是返回类型吗? const int i = 5; int b = const_cast(i); 是const_cast返回 in
我有一个函数 static bool Validate(const char& letter, string& aWord) 我需要调用它 Validate(letter, aWord); // wh
这个问题在这里已经有了答案: Two different values at the same memory address (7 个答案) 关闭 5 年前。 考虑以下代码: 我声明了一个新的引用端
我有一个大型库,它实现了一些不可变的数据结构。可以想象,其中几乎所有内容都是 const合格的。有一些选择部分不是 const,例如引用计数器。为了处理嵌入结构中的引用计数器,这些引用计数器只能通过
长话短说是否适用于: mapm; m.insert( make_pair( 1, 40 ) ); for( map::iterator it = m.begin(); it !
据我所知,在类中创建常量函数对于读/写编译器优化很有用。 类中的常量函数意味着类成员在函数执行期间将保持不变。但是,您可以通过 const 强制转换隐式参数来绕过此问题(当然,这是一种非常糟糕的做法)
这个问题在这里已经有了答案: Two different values at the same memory address (7 个答案) 关闭 5 年前。 考虑以下代码: 我声明了一个新的引用端
考虑以下 C++03 程序: #include struct T { mutable int x; T() : x(0) {} }; void bar(int& x) { x
我正在查看我即将开始使用的 API 的一些示例代码。以下模式让我有点困惑: char* str; str = const_cast("Hello World"); printf("%s ", str)
我是一名优秀的程序员,十分优秀!