C++中成员初始化列表的使用
C++在类的构造函数中,可以两种方式初始化成员数据(data member)。
{
private:
int x,y;
public:
point(int m=0,int n=0)
{
x=m;
y=n;
}
{
return x;
}
int GetY()
{
return y;
}
};
{
}
{
protected:
int m_x,m_y;
public:
point(int m=0,int n=0)
{
m_x = m;
m_y = n;
printf("constructor called!/n");
}
point(point& p)
{
m_x = p.GetX();
m_y = p.GetY();
printf("copy constructor called!/n");
}
int GetX()
{
return m_x;
}
int GetY()
{
return m_y;
}
};
{
private:
point m_p;
int m_z;
public:
point3d(point p, int k)
{
m_p = p; //这里是对m_p的赋值
m_z=k;
}
point3d(int i,int j, int k):m_p(i,j) // 相当于 point m_p(i,j)这样对m_p初始化
{
m_z=k;
}
void Print()
{
printf("%d,%d,%d /n",m_p.GetX(),m_p.GetY(),m_z);
}
};
{
point p(1,2); //先定义一个2D坐标
{
point p(1,2);
}
{
private:
const int a;
int& b;
// base(int m, int n)
// {
// a = m;
// b = n;
// }
{}
};
{
base ba(1,2);
}
---------------------------
C++中成员初始化列表的使用的更多相关文章
- (转) C++中成员初始化列表的使用
C++在类的构造函数中,可以两种方式初始化成员数据(data member). 1,在构造函数的实现中,初始类的成员数据.诸如: class point{private: int x,y;public ...
- C++中使用初始化列表的情况
http://blog.csdn.net/iceshirley/article/details/5688696 要理解这个问题,从概念上,我们要知道一点,那就是构造函数的执行过程会分成两个阶段:隐式或 ...
- C++:用成员初始化列表对数据成员初始化
1.在声明类时,对数据成员的初始化工作一般在构造函数中用赋值语句进行. 例如: class Complex{ private: double real; double imag; public: Co ...
- C++类的成员初始化列表的相关问题
在以下四中情况下,要想让程序顺利编译,必须使用成员初始化列表(member initialization list): 1,初始化一个引用成员(reference member): 2,初始化一个常量 ...
- C++: 类成员初始化列表语法
类的成员初始化列表的初始化的基本语法,类的构造函数还可以运用此语法为其变量初始化: class Class { private: int a; int b; char ch; public: Cl ...
- C++ 成员初始化列表
1.什么是成员初始化列表 #include<iostream> #include<string> using namespace std; class Weapon { pri ...
- C++的成员初始化列表和构造函数体(以前未知)
成员的初始化列表和构造函数在对成员指定初值方面是不一样的.成员初始化列表是对成员初始化,而构造函数,是对成员赋值 成员初始化列表使用初始化的方式来为数据成员指定初值, 而构造函数的函数体是通过赋值的方 ...
- c++类 用冒号初始化对象(成员初始化列表)
c++类 用冒号初始化对象(成员初始化列表) 成员初始化的顺序不同于它们在构造函数初始化列表中的顺序,而与它们在类定义中的顺序相同 #include<iostream> ; using n ...
- C++ 使用成员初始化列表的一个小坑
注意在成员列表中初始化的顺序并不是列表顺序 而是: 在类中声明的顺序! EventLoop::EventLoop() :looping(false), quit(false),_tid(curThre ...
随机推荐
- Ext3.4实现增删查改(form版)
var TaskPolicyStore = new Ext.data.JsonStore( { autoLoad : false, url : 'PolicyServlet?method= ...
- e808. 建立菜单栏,菜单,菜单项
When the user selects a menu item, it fires an action event. // Create the menu bar JMenuBar menuBar ...
- swing包含了各种组件的类
javax.swing 最常用的pachage,包含了各种swing组件的类 javax.swing.border 包含与swing组件外框有关的类 javax..swing.colorchooser ...
- ASP.NET MVC使用Oauth2.0实现身份验证
随着软件的不断发展,出现了更多的身份验证使用场景,除了典型的服务器与客户端之间的身份验证外还有,如服务与服务之间的(如微服务架构).服务器与多种客户端的(如PC.移动.Web等),甚至还有需要以服务的 ...
- (转)linux用文件锁实现保证一个程序只能启动一个进程
#include <stdio.h> #include <unistd.h>#include <fcntl.h>#include <errno.h>in ...
- C# 获取文件大小
当然了都需要引入System.IO这个命名空间 第一个: public static long GetDirectoryLength(string dirPath){//判断给定的路径是否存在,如果不 ...
- 理解端口与IP
理解IP和端口 端口,端口号,服务器端口------百科
- Material Design Support 8大控件介绍
TextInputLayout 显示提示信息 能够通过调用setError()在EditText以下显示一条错误信息 FloatingActionButton 悬浮操作按钮 Snackbar 相当于底 ...
- A potentially dangerous Request.Path value was detected from the client异常解决方案
场景: 当URL中存在“<,>,*,%,&,:,/”特殊字符时,页面会抛出A potentially dangerous Request.Path value was detect ...
- Phpcms V9单页添加自定义字段
说起文章自定义自段,大家都会想到 wordpress 的自定义字段,确实 wordpress 系统很强大,字段可以很灵活的在后台进行添加与更新,并能够很好的在前台进行调用,对于这点 phpcms v9 ...