static成员变量与返回对象的引用
(1)用static修饰类成员变量(属性),表明该变量是静态的,无论创建多少对象,都只创建一个一个静态属性副本,也就是对象们共享同一个静态属性,这个方法常用的一个用途就是用来计算程序调用了多少次这个类来创建对象也就是创建过多少个对象。
#ifndef TIME_H_
#define TIME_H_
#include <iostream>
using namespace std;
class Time
{
private:
int hours;
int minutes;
int seconds;
static int count;
public:
Time();
Time(int h = 0, int m = 0, int s = 0);
……
#include "Time.h"
#include <cstdlib>//支持abort()函数
int Time::count = 0;//初始化时不用加static
Time::Time()
{
hours = minutes = seconds = 0;
count++;
}
Time::Time(int h, int m, int s)
{
if (h < 0 || h > 24 || m > 60 || m < 0 || s > 60 || s < 0)
{
cout << "初始化参数输入有误,程序终止!" << endl;
abort();
}
hours = h;
minutes = m;
seconds = s;
count++;
}
同时,若非整形/枚举型const的静态属性,都必须在实现文件(.cpp)中进行初始化,且初始化时独立于其他成员函数的,也就是不能在构造函数中初始化静态属性:
int Time::count = 0;//初始化时不用加static
若是整形/枚举型const的静态属性,才可以并且必须在声明文件(.h)中初始化,且在声明中初始化的整形/枚举型static属性,必须声明为const类型。
(2)同时这里说一下“返回对象的引用”这个方法,
Time Time::max(const Time &t1, const Time &t2)
{
return t1;
}
这种返回方法需要创建一份t1对象的副本(调用复制构造函数),效率比较低。而下面这个方法:
const Time& Time::max(const Time &t1, const Time &t2)
{
return t1;
}
则返回的是对象t1的引用(别名),效率更高,同时因为t1在参数中是声明为const类型的,所以返回值也必须声明为const(这里的const表明我返回给你的东西你不能进行修改),否则报错。
(3)最后再讲一个小知识点,就是用new关键字来创建类的对象,
Time *a = new TIme();
这句话之后必须同时搭配
delete a;
进行使用,而非系够函数自动delete。
这个知识点援引自《C++ primer plus》(第六版中文版)12.5.1节:
Time.h:
#pragma once
/*
* Time.h
*
* Created on: 2016-4-16
* Author: lvlang
*/
#ifndef TIME_H_
#define TIME_H_
#include <iostream>
using namespace std;
class Time
{
private:
int hours;
int minutes;
int seconds;
static int count;
public:
Time();
Time(int h = 0, int m = 0, int s = 0);//如果不传入值则自动初始化为0
void AddHr(int h);
void AddMin(int m);
void reset(int h = 0, int m = 0, int s = 0);
void show()const;//const在这里表明本函数不会也不能去修改属性
Time sum(const Time &t)const; //传引用比传值效率高
Time operator+(const Time &t)const;
const Time &max(const Time &t1, const Time &t2);
~Time();
};
#endif /* TIME_H_ */
Time.cpp:
/*
* Time.cpp
*
* Created on: 2016-4-16
* Author: lvlang
*/
#include "Time.h"
#include <cstdlib>//支持abort()函数
int Time::count = 0;
Time::Time()
{
hours = minutes = seconds = 0;
count++;
}
Time::Time(int h, int m, int s)
{
if (h < 0 || h > 24 || m > 60 || m < 0 || s > 60 || s < 0)
{
cout << "初始化参数输入有误,程序终止!" << endl;
abort();
}
hours = h;
minutes = m;
seconds = s;
count++;
}
void Time::AddHr(int h)
{
hours = (hours + h) % 24;
}
void Time::AddMin(int m)
{
int temp = this->minutes + m;
this->hours += temp / 60;
this->minutes = temp % 60;
}
void Time::reset(int h, int m, int s)
{
if (h < 0 || h > 24 || m > 60 || m < 0 || s > 60 || s < 0)
{
cout << "参数输入有误,程序终止!" << endl;
abort();
}
this->hours = h;
this->minutes = m;
this->seconds = s;
}
void Time::show()const
{
cout << "Hours: " << this->hours << " Minutes: " << this->minutes
<< " Seconds: " << this->seconds <<" Count: "<<count<< endl;
}
Time Time::sum(const Time &t)const
{
Time temp(0,0,0);
int ts = (this->seconds + t.seconds) / 60;
temp.seconds = (this->seconds + t.seconds) % 60;
temp.minutes = (this->minutes + t.minutes + ts) % 60;
int tm = (this->minutes + t.minutes + ts) / 60;
temp.hours = (this->hours + t.hours + tm) % 24;
return temp;
//return *this;//返回当前对象(this为指向当前对象的指针)
}
Time Time::operator+(const Time &t)const
{
Time temp(0,0,0);
int ts = (this->seconds + t.seconds) / 60;
temp.seconds = (this->seconds + t.seconds) % 60;
temp.minutes = (this->minutes + t.minutes + ts) % 60;
int tm = (this->minutes + t.minutes + ts) / 60;
temp.hours = (this->hours + t.hours + tm) % 24;
return temp;//return之后会自动调用一次析构函数把temp的空间回收
}
const Time& Time::max(const Time &t1, const Time &t2)
{
return t1;
}
Time::~Time()
{
cout << "析构函数被调用" << endl;
}
main.cpp
#include "Time.h"
int main()
{
Time time(10,10,10);
time.show();
time.AddHr(2);
time.show();
time.AddMin(20);
time.show();
Time t(1, 1, 1);
t.show();
t.reset(9, 9, 9);
t.sum(time);
t.show();
/*t = t + time;
t.show();*/
return 0;
}
static成员变量与返回对象的引用的更多相关文章
- C++中的static 成员变量的一些注意点
C++中的static成员变量主要用来为多个对象共享数据 例: #include <iostream> using namespace std; class Student{ public ...
- static 成员变量、static 成员函数、类/对象的大小
一.static 成员变量 对于特定类型的全体对象而言,有时候可能需要访问一个全局的变量.比如说统计某种类型对象已创建的数量. 如果我们用全局变量会破坏数据的封装,一般的用户代码都可以修改这个全局变量 ...
- 牛客网Java刷题知识点之关键字static、static成员变量、static成员方法、static代码块和static内部类
不多说,直接上干货! 牛客网Java刷题知识点之关键字static static代表着什么 在Java中并不存在全局变量的概念,但是我们可以通过static来实现一个“伪全局”的概念,在Java中st ...
- java static成员变量方法和非static成员变量方法的区别
这里的普通方法和成员变量是指,非静态方法和非静态成员变量首先static是静态的意思,是修饰符,可以被用来修饰变量或者方法. static成员变量有全局变量的作用 非static成员变量则 ...
- C++11类内static成员变量声明与定义
众所周知,将一个类内的某个成员变量声明为static型,可以使得该类实例化得到的对象实现对象间数据共享. 在C++中,通常将一个类的声明写在头文件中,将这个类的具体定义(实现)写在cpp源文件中. 因 ...
- Runtime之成员变量&属性&关联对象
上篇介绍了Runtime类和对象的相关知识点,在4.5和4.6小节,也介绍了成员变量和属性的一些方法应用.本篇将讨论实现细节的相关内容. 在讨论之前,我们先来介绍一个很冷僻但又很有用的一个关键字:@e ...
- 类的static成员变量和成员函数能被继承吗
1. 父类的static变量和函数在派生类中依然可用,但是受访问性控制(比如,父类的private域中的就不可访问),而且对static变量来说,派生类和父类中的static变量是共用空间的,这点 ...
- static成员变量
可以创建一个由同一个类的所有对象共享的成员变量.要创建这样的成员,只需将关键字 static 放在变量声明的前面,如下面的类所示: class StatDemo { private: static i ...
- C++ 虚指针、成员变量与类对象的偏移地址
先给出一段代码实现 #include <iostream> using namespace std; class animal { protected: int age; public: ...
随机推荐
- 结合个人经历总结的前端入门方法 (转自https://github.com/qiu-deqing/FE-learning)
结合个人经历总结的前端入门方法 (https://github.com/qiu-deqing/FE-learning),里面有很详细的介绍. 之前一直想学习前端的,都不知道怎么下手都一年了啥也没学到, ...
- yii2-basic后台管理功能开发之五:用户权限的控制
需求:后台有两种权限+管理员+用户 区别:可以访问的页面不同 解决方法:用户表添加role字段,使用到的是yii2 存取控制过滤器(ACF)方法. >1在web.php中配置授权的类,有两种,我 ...
- node.js的优缺点
node.js的优缺点 优点: 1. 采用事件驱动,异步编程,为网络服务而设计. 2. node.js非阻塞模式的IO处理给node.js带来在相对较低的资源耗用下的高性能与出众的负载能力. 3. n ...
- setWinldowRgn
CRgn(HRGN) SetWindowRgn setRgn{ CRgn rgnRect; CRgn rgnHole; //获取窗口大小 CRect rcWnd; GetWindowRect(& ...
- 为OpenEdx中lms注册模块扩展字段
最近遇到需求,需要扩展注册的字段,例如新增手机mobile字段 1.打开register.html文件 路径为\edxapp\edx-platform\lms\templates\register ...
- Apache与nginx优缺点对比
1.nginx相对于Apache优点: nginx轻量级,同样起web服务,比apache占用更少的内存资源: 抗并发,nginx处理请求是异步非阻塞型的,Apache处理请求是阻塞型的,所以在处理高 ...
- WHMCS成功安装和使用方法及添加支付宝,PayPal收款教程
一.WHMCS安装前准备 1.WHMCS官网: 1.官方首页:http://www.whmcs.com/ 2.WHMCS需要安装在一个带MysqL数据库的PHP服务器中,一般地我们日常安装的VPS控制 ...
- 使用面向 iOS 的本机插件扩展 PhoneGap
本文细致探讨了 Xcode(以 iOS 设备为目标)中的 PhoneGap(也称为 Apache Cordova)应用程序本机插件.如果您刚开始接触 PhoneGap 或者需要回顾 PhoneGap ...
- docker index服务概述
index顾名思义“索引”,index服务主要提供镜像索引以及用户认证的功能.当下载一个镜像的时候,首先会去index服务上 做认证,然后查找镜像所在的registry的地址并放回给docker客户端 ...
- js控制键盘只能输入数字和退格键,delete键
function numbText(e){ if(e&& e.stopPropagation){ code= e.which; }else{ code= window.event.ke ...