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: ...
随机推荐
- ireport5.6+jasperreport6.3开发(五)--以javabean为基准的报表开发(action关联)
这里的是定方法主要参照sturts2-jasperreport-plugin的完成方法(其实就是抄的) PDF的样子是这样的两页的pdf 然后action的配置是这样的(不要在意格式) @Parent ...
- poj2965 The Pilots Brothers' refrigerator
题目链接:http://poj.org/problem?id=2965 分析:1.这道题和之前做的poj1753题目差不多,常规思路也差不多,但是除了要输出最少步数外,还要输出路径.做这道题的时候在怎 ...
- bean生命周期
一.Bean的定义Spring通常通过配置文件定义Bean.如:xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:s ...
- vs2015打包winform程序遇到的一系列问题
1.因为打包的时候用的是release版本的东西,所以就先把项目按release编译一下,然后一大波bug,后来修改了生成目标平台为x86,我的解决方案里面加上安装部署项目共5个(ui配置:活动rel ...
- asp.net发邮件功能
protected void SendMail() { try { string CreaterName = ""; string examiner = ""; ...
- Windows Store App Image开发示例
通过上面的介绍,读者已经了解了Image对象及ImageBrush对象的使用方法和常用属性,在实际的开发工作中,比较常用的是Image对象,下面以一个幼儿园识物识字卡应用为例,来帮助读者更好的理解Im ...
- Flapper Bird的学习笔记(三)
因为我有一个超屌的梦想,所以就绝不会做一个孬种的追梦人! 完成音效的添加 单例模式 游戏的状态切换 1. 单例模式 首先呢,说一下单例模式.何为单例?单例模式是一种常用的软件设计模式.在它的核心结构中 ...
- Android中日期函数Calendar的一些用法和注意事项
1.月份获取时加1 Canlendar.MONTH + 1 因为使用的是罗马历,Calendar.MONTH返回的数值不是一年中月份的值,而是当前月份距离第一个月份的差值 如:当前月份为9月份,距离1 ...
- CSS表达式
一直以来我们被教育说CSS Expression是个坏东西,很影响性能,应该禁止使用,但是如果仔细想想CSS表达式影响性能的原因,规避掉影响性能的写法,CSS表达式还是能给我们带来一些惊喜的.CSS表 ...
- Jupyter Notebook 27绝技——27 Jupyter Notebook tips, tricks and shortcuts
转载自:https://www.dataquest.io/blog/jupyter-notebook-tips-tricks-shortcuts/ Jupyter notebook, formerly ...