构造函数,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 ...
随机推荐
- 把数据输出到Word (非插件形式)
项目开发过程中,我们要把数据以各种各样的形式展现给客户.把数据以文档的形式展现给客户相信是一种比较头疼的问题,如果没有好的方法会 使得我的开发繁琐,而且满足不了客户的需求.接下来我会通过两种开发方式介 ...
- 易货beta版本测试报告
测试 对于服务器端我们是进行了单元测试 对于客户端我们使用的是在线的云测工具对app进行了包括安装,启动,具体功能以及ui方面的测试. 另外,对于客户端,我们还进行了对细节功能的人工测试 功能需求编号 ...
- :after和:before的作用及使用方法
1. :before 和 :after 的主要作用是在元素内容前后加上指定内容,示例: HTML代码: <p>你好</p> CSS代码: p:before{ content: ...
- Deep Learning 25:读论文“Network in Network”——ICLR 2014
论文Network in network (ICLR 2014)是对传统CNN的改进,传统的CNN就交替的卷积层和池化层的叠加,其中卷积层就是把上一层的输出与卷积核(即滤波器)卷积,是线性变换,然后再 ...
- [SQL] SQL 查出一张表中重复的所有记录
在A表中存在一个字段“AccountId”,而且不同记录之间的“AccountId”值有可能会相同,现在就是需要查询出在该表中的各记录之间,“AccountId”值存在重复的项,这里count记录该字 ...
- Android之Dialer之紧急号码
Android之Dialer之紧急号码 e over any other (e.g. supplementary service related) number analysis. a) 112 an ...
- Selenium ide录制回放错误Timed out after 30000ms
[error] Timed out after 30000ms 该问题可能是速度控制条播放速度过快导致,调整播放速度至slow
- 不断弹出svchost.exe错误框
同事的一台电脑,xp系统,启动后就弹出svchost错误的对话框,不论确定还是取消,关闭后立刻又弹出. 打开任务管理器,尝试对一些后安装的软件结束进程,结束一个,关闭一次,看看结束哪一个,关闭后不再弹 ...
- Sort Colors [LeetCode]
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- 拆分SharePoint 2013 中CreatedModifiedInfo 的时间
最近在自定义DisForm.aspx页面时 发现 创建时间信息无法进行拆分,人事MM只想要修改时间,去掉创建人,创建时间和修改人. 于是我的重新研究下在SPD里面如何去拆分这个时间. 在详情页面上找到 ...