const_cast<type-id>(expression)

//**********************//
类类型
class B{
    public:
        int m_num;
        B():m_num(50){}
};                                                                                                                                     
void foo(void) {

    const B* b1 = new B();
    B* b2 = const_cast<B*>(b1);
    b2->m_num = 200;
    cout <<"b1:" << b1->m_num << endl;//200
    cout <<"b2:" << b2->m_num << endl;//200

    const B b3;
    B b4 = const_cast<B&>(b3);
    b4.m_num = 300;
    cout << "b3:" << b3.m_num << endl;//50
    cout << "b4:" << b4.m_num << endl;//300
}
//************************//
//************************//
基本类型
void foo(){
    const int a = 100;
    int* p1 = const_cast<int*>(&a);
    *p1 = 200;
    cout << *p1 << endl;//200
    cout << a << endl;//100

    const int* p2 = new int(100);
    int* p3 = const_cast<int*>(p2);
    *p3 = 200;
    cout << *p2 << endl;//200                                                   
    cout << *p3 << endl;//200
}
//************************//

你会发现:

A:可以为基本类型或者类类型;

const A a;随便怎么修改a都不会变化

const A* p = new A();去掉p的const属性后,*p就变化了.

//*****************//
class A{
    public:                                                                     
        A(){
            m_num=1;
        }   
        int m_num;
};
void foo (void){
    A a;
    const A &r = a;
    A a1 = const_cast<A&>(a);
    a1.m_num = 200;
    cout << a1.m_num << endl;//200
    cout << a.m_num << endl;//1
}
//****************//

const_cast<type-id>(expression)中,type-id只能为指针或引用,其他的都错,这个表达式即可以去除

expression中的const属性或volatil属性,还能增加const属性或者volatil属性

const int i = 10;

int i1 = const_cast<int>(i) //错误

增加const属性与volatil属性相反.

时间: 2024-08-20 15:47:54

const_cast<type-id>(expression)的相关文章

[Fatal Error] :3:13: Open quote is expected for attribute &quot;{1}&quot; associated with an element type &quot;id&quot;.

用DOM解析XML时出现了如下错误: [Fatal Error] :3:13: Open quote is expected for attribute "{1}" associated with an  element type  "id".org.xml.sax.SAXParseException: Open quote is expected for attribute "{1}" associated with an  element t

Sending &#39;ViewController *const __strong&#39; to parameter of incompatible type &#39;id&lt;***Delegate&gt;&#39;

iphone开发出现警告:Sending 'ViewController *const __strong' to parameter of incompatible type 'id<***Delegate>' 原因是没有在头文件实现相应协议. 解决方法是在头文件中 @interface 一行后面加上 <协议名> Sending 'ViewController *const __strong' to parameter of incompatible type 'id<***

implicitly declaring library function &#39;objc_msgSend&#39;with type &#39;(id,SEL,...)&#39; 警告

之前一直用objc_msgSend,但是没注意apple的文档提示,所以突然objc_msgSend crash了. 之前32位的时候没问题,然后转换为64位之后就会发生EXC_BAD_ACCESS问题. 当然apple再文档([64-Bit Transition Guide for Cocoa Touch中有](https://developer.apple.com/library/prerelease/ios/documentation/General/Conceptual/CocoaTou

typeid, const_cast&lt;Type&gt;的使用

1 #include <bits/stdc++.h> 2 using namespace std; 3 4 class A { 5 public : 6 void Show() { 7 cout << "A" << endl; 8 } 9 } ; 10 11 class CC { 12 public : 13 void Show() { 14 cout << "CC" << endl; 15 } 16 }

[java] javax.el.PropertyNotFoundException: Property &#39;id&#39; not found on type bean.Student

问题提出: 在使用MyEclipse开发Java Web时,调用DAO和Java Bean出现了如下错误: 严重: Servlet.service() for servlet [jsp] in context with path [/JDBCbyDao] threw exception [An exception occurred processing JSP page /student.jsp at line 37 34: 35: <c:forEach items="${ student

C++ static_cast const_cast dynamic_cast 和reinterpret_cast的区别

1.static_cast Operator The expression static_cast < type-id > ( expression ) converts expression to the type of type-id based solely on the types present in the expression. No run-time type check is made to ensure the safety of the conversion. Synta

C++ 转型操作符 【1】static_cast 和const_cast

[c++转型操作符] goto语句一直被视为程序设计上的"贱民",低阶转型动作它有的一拼,虽然它任然能够苟延残喘,不过,旧式的C转型方式并非是唯一选择. 旧式转型的语法结构: 由一对小括号加上一个对象名称(标识符)组成,而小括号和对象名称在C++的任何地方都能够被使用,因此,我们简直无法回答最基本的转型相关问题"这个程序张有使用过任何转型动作吗?" <span style="font-size:18px;">(type)express

【C++注意事项】5 Top-level const , The auto and decltype Type Specifier

top-level const As we've seen, a pointer is an object that can point to a different object. As a result, we can talk independently about whether a pointer is const and whether the objects to which it can point are const. we use the top-level const to

Rhythmk 学习 Hibernate 03 - Hibernate 之 延时加载 以及 ID 生成策略

Hibernate 加载数据 有get,跟Load 1.懒加载: 使用session.load(type,id)获取对象,并不读取数据库,只有在使用返回对象值才正真去查询数据库. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 @Test    public void  test1()    {        Session session = null;         try {             session = Hiber

格式工厂(七)Type Traits

版权声明:本文为博主原创文章,未经博主允许不得转载. 识别变量的type id,返回true或false,举一个简单使用的例子 template <typename T> void type_traits_output(const T& x) { cout << "\ntype traits for type : " << typeid(T).name() << endl; cout << "is_void\