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: ...
随机推荐
- (转)apache和nginx的区别
nginx 相对 apache 的优点: 轻量级,同样起web 服务,比apache 占用更少的内存及资源 抗并发,nginx 处理请求是异步非阻塞的,而apache 则是阻塞型的,在高并发下ngin ...
- HTML5中引入的关键特性
新特性 描述 accesskey 定义通过键盘访问元素的快捷键 contenteditable 该特性设置为true时,浏览器应该允许用户编辑元素的内容.不指定变化后的内容如何保存 contextme ...
- JAVA语法02之课程问题解决
(一)示例程序+运行结果: ①EnumTest.java public class EnumTest { public static void main(String[] args) { Size s ...
- sqlserver 事务日志过大 收缩方法解决方案
sqlserver 事务日志过大,可能会导致备份失败或者数据库出现异常,所以要定期清除sqlserver 事务日志 建议:为了防止日志文件无限扩大,可以对日志文件作一些限制. 清除sqlserver事 ...
- 《精通C#》委托与事件(10章)
委托可用来解耦以及状态变化的实时通知,以及其他的一些作用,但是经验所限,目前还未遇见.网上的大多数例子都是类似于,使用委托,然后用console返回方法的返回值,有时候会在想,委托的通知如果是这样的话 ...
- 设置默认python模块源
可以在Linux编辑~/.pip/pip.conf或者在Windows下编辑%HOME%\pip\pip.ini,内容如下:[global]index-url = http://pypi.douban ...
- Python笔记总结week2
1. 关于Python程序执行原理:
- UISegmentControl
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...
- 错误 java.lang.ClassCastException: com.ylpw.sms.YZZYSenderUtil cannot be cast to ResourceBundle
出现错误: java.lang.ClassCastException: com.ylpw.sms.YZZYSenderUtil cannot be cast to ResourceBundle 百度搜 ...
- POJ1635 树的最小表示
/*zoj1990Subway Tree Systems题目大意:初始时站在树的根节点,若朝着远离根的方向走,记录“”,接近根的方向走记录“”.并且树的每一条边只能来回走一次(即向下和返回).一个合法 ...