1. /*******************************************************************************
  2. * 版权所有:
  3. * 模 块 名:
  4. * 文 件 名:class_default_constructor_for_const_member.cpp
  5. * 实现功能:
  6. * 作 者:XYZ
  7. * 版 本:V1.0
  8. * 日 期:2013.08.12
  9. * 其他说明:xiao13149920@foxmail.com
  10. ********************************************************************************/
  11. // class_default_constructor_for_const_member.cpp
  12. #include<iostream>
  13. #include<cstdlib>
  14. #include<cstring>
  15. using namespace std;
  16.  
  17. //#define INIT
  18.  
  19. class CBase
  20. {
  21. private:
  22. #ifdef INIT
  23. const int m_const_i;
  24. #endif
  25. static int m_static_i;
  26. const static int m_const_static_i /*= 100*/;
  27.  
  28. public:
  29. void print();
  30. };
  31.  
  32. int CBase::m_static_i = ;
  33. const int CBase::m_const_static_i = ;
  34.  
  35. void CBase::print()
  36. {
  37. #ifdef INIT
  38. cout<<"ci="<<ci<<endl;
  39. #endif
  40. cout<<"m_static_i="<<m_static_i<<endl;
  41. cout<<"m_const_static_i="<<m_const_static_i<<endl;
  42. }
  43.  
  44. class B
  45. {
  46. private:
  47. #ifdef INIT
  48. CBase& cb;
  49. #endif
  50. };
  51.  
  52. int main()
  53. {
  54. // test for default constructor
  55. CBase cb;
  56. cb.print();
  57. B b;
  58.  
  59. // test for const char * p = str._cstr();
  60. string str = "hello world";
  61. const char * p = str.c_str();
  62. str[]='H';
  63. str.append("-----------------\n");
  64. char parr[];
  65. memset(parr, , sizeof(parr));
  66. strcpy(parr, p);
  67. cout<<"p="<<p<<endl;
  68. cout<<"str="<<str<<endl;
  69. cout<<"parr="<<parr<<endl;
  70.  
  71. return ;
  72. }

1. 编译器会为每个类自动生成一个缺省的构造函数,拷贝构造,赋值函数. 但有const 数据成员(无static组合的), 引用数据成员是一个例外.即代码中的55,57行.

2. 不可把string.c_str()的内容赋值给const char* p, 即代码中的61行

3. const static 数据成员也可以在类中初始化. 如行26.

4. 新增若是基类的拷贝构造或者复制函数为私有函数, 那么编译器也不会为派生类生成一个缺省拷贝构造和赋值函数

构造函数,const char*与c_str的更多相关文章

  1. string、const char*、 char* 、char[]相互转换

    转化总结如下: 目标格式 源格式 string const char* char* char[] string NULL const char*=string.c_str(); const char* ...

  2. VS2017中遇到不存在从string到const char*的转换函数的解决方法

    使用c_str()函数 c_str函数的返回值是const char*. c_str()函数返回一个指向正规C字符串的指针, 内容与本string串相同. 这是为了与c语言兼容,在c语言中没有stri ...

  3. c++ const char *c_str(); 坑的学习

    语法: const char *c_str(); c_str()函数返回一个指向正规C字符串的指针, 内容与本string串相同. 这是为了与c语言兼容,在c语言中没有string类型,故必须通过st ...

  4. QStringLiteral(源代码里有一个通过构造函数产生的从const char*到QString的隐式转换,QStringLiteral字符串可以放在代码的任何地方,编译期直接生成utf16字符串,速度很快,体积变大)

    原作者: Olivier Goffart 点击打开链接http://woboq.com/blog/qstringliteral.html 译者: zzjin 点击打开链接http://www.tuic ...

  5. 实战c++中的string系列--string与char*、const char *的转换(data() or c_str())

    在project中,我们也有非常多时候用到string与char*之间的转换,这里有个一我们之前提到的函数 c_str(),看看这个原型: const char *c_str(); c_str()函数 ...

  6. 【C++】int、const char*、char*、char、string之间的转换

    #include "stdafx.h" #include<string> #include<vector> #include<iostream> ...

  7. string,const char*,char*之间的相互转换

    1. string转const char* string s = "abc"; const char* c_s = s.c_str(); 2. const char*转string ...

  8. C++ char*,const char*,string的相互转换

    1. string转const char* string s ="abc";constchar* c_s = s.c_str(); 2. const char*转string   ...

  9. const char* <----- > string

    (1) const char*      <-----     string const char* const_txt_path=txt_path.c_str(); (2)  string  ...

随机推荐

  1. CSUOJ_1001

    /* * Title : A+B(II) * Data : 2016/11/09 * Author : Andrew */ #include <iostream> #include < ...

  2. 转:Eclipse插件开发之TreeViewer

    http://www.tuicool.com/articles/e6fmE3R contentprovider在插件开发和RCP(Rich Client Platform)开发中常常被用到,譬如你要创 ...

  3. contiki-process_run()

    process_run()函数位于main函数中 ) { do { } ); idle_count++; } 找到函数的声明处: /** * Run the system once - call po ...

  4. js触发按钮点击事件

    js触发按钮点击事件 博客分类: javascript   模拟JS触发按钮点击功能 <html> <head> <title>usually function&l ...

  5. ES6(一)ECMAscript6介绍

    nvm-windows Node.js是JavaScript语言的服务器运行环境,对ES6的支持度比浏览器更高.通过Node,可以体验更多ES6的特性.建议使用版本管理工具nvm,来安装Node,因为 ...

  6. C# 中正确实现 IDisposable 接口

    作用 此接口的主要用途是释放非托管资源. 当不再使用托管对象时,垃圾回收器会自动释放分配给该对象的内存. 但无法预测进行垃圾回收的时间. 另外,垃圾回收器对窗口句柄或打开的文件和流等非托管资源一无所知 ...

  7. PHP xml 转换为 array

    retrun json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), tru ...

  8. application.xml dubbo报错解决方案

  9. Windows Phone 10如何借Windows 10的东风

    距微软发布Windows Phone 7已经四年多了,WinPhone的市场份额一直萎糜不前.去年微软收购诺基亚,如特洛伊木马般戏剧,却没有挽救WinPhone,甚至出现下滑,已经不足3%,已经基本被 ...

  10. shell脚本中的几个括号总结(小括号/大括号/花括号)--from:http://www.cnblogs.com/hanyan225/archive/2011/10/06/2199652.html

    在Shell中的小括号,大括号结构和有括号的变量,命令的用法如下: 1.${var}2.$(cmd)3.()和{}4.${var:-string},${var:+string},${var:=stri ...