构造函数,const char*与c_str
/*******************************************************************************
* 版权所有:
* 模 块 名:
* 文 件 名:class_default_constructor_for_const_member.cpp
* 实现功能:
* 作 者:XYZ
* 版 本:V1.0
* 日 期:2013.08.12
* 其他说明:xiao13149920@foxmail.com
********************************************************************************/
// class_default_constructor_for_const_member.cpp
#include<iostream>
#include<cstdlib>
#include<cstring>
using namespace std; //#define INIT class CBase
{
private:
#ifdef INIT
const int m_const_i;
#endif
static int m_static_i;
const static int m_const_static_i /*= 100*/; public:
void print();
}; int CBase::m_static_i = ;
const int CBase::m_const_static_i = ; void CBase::print()
{
#ifdef INIT
cout<<"ci="<<ci<<endl;
#endif
cout<<"m_static_i="<<m_static_i<<endl;
cout<<"m_const_static_i="<<m_const_static_i<<endl;
} class B
{
private:
#ifdef INIT
CBase& cb;
#endif
}; int main()
{
// test for default constructor
CBase cb;
cb.print();
B b; // test for const char * p = str._cstr();
string str = "hello world";
const char * p = str.c_str();
str[]='H';
str.append("-----------------\n");
char parr[];
memset(parr, , sizeof(parr));
strcpy(parr, p);
cout<<"p="<<p<<endl;
cout<<"str="<<str<<endl;
cout<<"parr="<<parr<<endl; return ;
}
1. 编译器会为每个类自动生成一个缺省的构造函数,拷贝构造,赋值函数. 但有const 数据成员(无static组合的), 引用数据成员是一个例外.即代码中的55,57行.
2. 不可把string.c_str()的内容赋值给const char* p, 即代码中的61行
3. const static 数据成员也可以在类中初始化. 如行26.
4. 新增若是基类的拷贝构造或者复制函数为私有函数, 那么编译器也不会为派生类生成一个缺省拷贝构造和赋值函数
构造函数,const char*与c_str的更多相关文章
- string、const char*、 char* 、char[]相互转换
转化总结如下: 目标格式 源格式 string const char* char* char[] string NULL const char*=string.c_str(); const char* ...
- VS2017中遇到不存在从string到const char*的转换函数的解决方法
使用c_str()函数 c_str函数的返回值是const char*. c_str()函数返回一个指向正规C字符串的指针, 内容与本string串相同. 这是为了与c语言兼容,在c语言中没有stri ...
- c++ const char *c_str(); 坑的学习
语法: const char *c_str(); c_str()函数返回一个指向正规C字符串的指针, 内容与本string串相同. 这是为了与c语言兼容,在c语言中没有string类型,故必须通过st ...
- QStringLiteral(源代码里有一个通过构造函数产生的从const char*到QString的隐式转换,QStringLiteral字符串可以放在代码的任何地方,编译期直接生成utf16字符串,速度很快,体积变大)
原作者: Olivier Goffart 点击打开链接http://woboq.com/blog/qstringliteral.html 译者: zzjin 点击打开链接http://www.tuic ...
- 实战c++中的string系列--string与char*、const char *的转换(data() or c_str())
在project中,我们也有非常多时候用到string与char*之间的转换,这里有个一我们之前提到的函数 c_str(),看看这个原型: const char *c_str(); c_str()函数 ...
- 【C++】int、const char*、char*、char、string之间的转换
#include "stdafx.h" #include<string> #include<vector> #include<iostream> ...
- string,const char*,char*之间的相互转换
1. string转const char* string s = "abc"; const char* c_s = s.c_str(); 2. const char*转string ...
- C++ char*,const char*,string的相互转换
1. string转const char* string s ="abc";constchar* c_s = s.c_str(); 2. const char*转string ...
- const char* <----- > string
(1) const char* <----- string const char* const_txt_path=txt_path.c_str(); (2) string ...
随机推荐
- Git_1基础操作,从安装到提交完成(windows)
github地址:https://github.com/zhangsai521314/Git 1:安装Git Bash(https://git-scm.com/),安装一路NEXT. 2:目录架构: ...
- iOS - Share 分享/第三方登录
1.系统方式创建分享 按照下图在 Info.plist 文件中将 Localization native development region 的值改为 China.如果不设置此项弹出的分享页面中显示 ...
- spring+缓存
1.配置ehcache.xml <?xml version="1.0" encoding="UTF-8"?> <ehcache updateC ...
- Linux 系统时间和硬件时间
linux 的系统时间有时跟硬件时间是不同步的 Linux时钟分为系统时钟(System Clock)和硬件(Real Time Clock,简称RTC)时钟.系统时钟是指当前Linux Kernel ...
- ajax 和 post 传多个参数值具体怎么写
ajax data:{id:id,name:name} 正确 data:{id:"001",name:"王俊凯"} 正确 data:{"id" ...
- Python学习---除法
python有两种除法,普通除法 a/b ,不论a,b精度 得到的都是浮点数. 4/2 = 2.0 3/5 = 0.6 floor除法,a//b , 得到一个舍弃小数位的整数结果,所以结果永远是 ...
- appml的安装与使用
来自w3cshool的中文文档:http://www.w3cschool.cc/appml/appml-tutorial.html 我们要做的是完整的例子,所以从这里http://www.w3csch ...
- dos2unix 命令
最近在学习shell编程,可是在<Linux程序设计>指定的网站上下载了源码,使用的时候却一直出问题.提示:"bash: ./here1:/bin/sh^M:损坏的解释器: 没有 ...
- MySQL JDBC/MyBatis Stream方式读取SELECT超大结果集
情景: 遍历并处理一个大表中的所有数据, 这个表中的数据可能会是千万条或者上亿条, 很多人可能会说用分页limit……但需求本身一次性遍历更加方便, 且Oracle/DB2都有方便的游标机制. 对DB ...
- shell算数运算
((i=$j+$k)) 等价于 i=`expr $j + $k`((i=$j-$k)) 等价于 i=`expr $j -$k`((i=$j*$k)) 等价于 i=`exp ...