c++保证对象在创建时正确初始化
来源:互联网 发布:破解苹果id软件 编辑:程序博客网 时间:2024/06/15 03:03
通常如果你使用c part of c++,而且初始化会招致运行期成本,那么你可以不保证初始化,但是一旦进入c++ no_part of c,那么你一定要保证对象正确初始化,这就是为什么在c中array没有初始化,但是到了C++中vector却在对象建立时候调用construtor 初始化,但是我们一定要避免出现C++对象的伪初始化(assigned初始化),下面代码说明,一下代码通过VS2008编译运行
#include <iostream>
#include<fstream>
#include <list>
#include <string>
#ifndef NUL
#define NUL '\0'
#endif
class phone_number
{
public:
phone_number():phone_list("0"){}
phone_number(const std::string& rhs):phone_list(rhs){}
friend std::ostream& operator<<(std::ostream&,const phone_number&);
private:
std::string phone_list;
};
std::ostream& operator<<(std::ostream& out, const phone_number& phone)
{
out<<phone.phone_list;
return out;
}
class man_infor
{
public:
man_infor(const std::string& name,const unsigned int age,const std::string& address, phone_number& phone_)
:the_name(name),the_age(age),the_address(address),the_phone_number(phone_)//推荐初始化方式
{
//the_name = name//这是典型的伪初始化,这样一方面会增加初始化负荷,另一方面违反C++初始化要在进入构造函数之前完成
}
int show(void)
{
std::cout<<the_name<<"\t"<<the_age<<"\t"<<the_address<<"\t"<<the_phone_number<<std::endl;
return NULL;
}
private:
std::string the_name;
unsigned int the_age;
std::string the_address;
phone_number& the_phone_number;
};
int main ()
{
phone_number mobile_phone("11");
man_infor wang("fish",22,"hfut",mobile_phone);
wang.show();
system("pause");
return NULL;
}
- c++保证对象在创建时正确初始化
- 如何保证只能在堆(heap)上创建对象?
- 如何保证只能在堆(heap)上创建对象?
- java创建对象时初始化
- 正确创建对象
- Java 创建对象时的初始化过程
- 对象创建及初始化
- 创建对象初始化过程
- java中在创建对象时候的初始化顺序
- Filebeat如何保证在日志文件被切割(或滚动rolling)时依然正确读取文件
- 使用正确的锁对象来保证线程安全
- 在Eclipse中运行Tomcat6源代码,保证正确的classpath
- 在Eclipse中运行Tomcat6源代码,保证正确的classpath
- objective-c对象初始化
- OBJECT-C 对象初始化
- Objective-C对象初始化
- Object-C 对象初始化
- c++vector对象初始化
- PO(采购)流程影响会计的环节分析
- 配置Tomcat 解压版
- 判断一个点是不是在三角形内
- mysql中的数据统计方法
- Activity之间的跳转---从一个Activity跳转到另一个Activity
- c++保证对象在创建时正确初始化
- 终于弄明白了傅里叶变换
- 优秀论文要素
- cocos2dx资料
- 自定义UITableViewCell
- ServletConfig对象和ServletContext对象
- GNOME3 fallback 模式下,在gnome-panel上增加multiload-applet
- 第一周
- lwip TCP传输速率振荡问题解决