c++-变量,this指针,全局函数,成员函数,自定义数组类
区分变量属于哪个对象
- c++对象管理模型初探
- C++类对象中的成员变量和成员函数是分开存储的,C中内存四区仍然有效
- C++编译器对普通成员函数的内部处理(隐藏this指针)
- this指针解决函数形参和类属性相同
- 类成员函数写const,修饰的是谁?
- 全局函数 pk 类成员函数
- 类成员函数返回指针 和 返回引用
- C++类对象中的成员变量和成员函数是分开存储的,C中内存四区仍然有效
- C++编译器对普通成员函数的内部处理(隐藏this指针)
- this指针解决函数形参和类属性相同
- 类成员函数写const,修饰的是谁?
- 全局函数 pk 类成员函数
- 类成员函数返回指针 和 返回引用
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
class Test
{
public:
Test(int i)
{
mI = i;
}
int getI()
{
//this就是指向调用改成员函数方法的对象地址
return this->mI;
//return mI;
}
private:
int mI;
};
/*
struct Test
{
int mI;
};
void Test_init(Test *pthis, int i)
{
pthis->mI = i;
}
int getI(struct Test *pthis)
{
return pthis->mI;
}
*/
int main(void)
{
Test t1(10);//Test(&t1, 10)
Test t2(20);
t1.getI();// getI(&t1)
return 0;
}
this指针
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
class Test
{
public:
Test(int k)
{
this->m_k = k;
}
int getK() const//成员函数尾部出现const 修饰是this指针
{
//this->m_k = 100; //this指针不是 const Test *
//this++;// this指针是一个常指针, Test *const
//this->m_k = 100;
//this = this + 1;
return this->m_k;
}
//static成员函数,只能返回static成员变量
static int s_getK()
{
//return m_k;
return s_k;
}
private:
int m_k;
static int s_k;
};
int Test::s_k = 0;
int main(void)
{
Test t1(10); //Test(&t1, 10);
Test t2(20);
return 0;
}
全局函数和成员函数
如果想返回一个对象的本身,在成员方法中,用*this返回
}
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
class Test
{
public:
Test(int a, int b)
{
this->a = a;
this->b = b;
}
void printT()
{
cout << "a = " << this->a << ", b=" << this->b << endl;
}
int getA()
{
return this->a;
}
int getB()
{
return this->b;
}
//成员方法
Test TestAdd(Test &another)
{
Test temp(this->a + another.a,this->b + another.b);
return temp;
}
//+= 方法
Test& TestAdd2(Test &another)
{
this->a += another.a;
this->b += another.b;
//this===>&t1
return *this;//如果想返回一个对象的本身,在成员方法中,用*this返回
}
private:
int a;
int b;
};
/*
//1 在全局提供一个两个Test想加的函数
Test TestAdd(Test &t1, Test &t2)
{
Test temp(t1.getA() + t2.getA(), t1.getB() + t2.getB());
return temp;
}
*/
int main(void)
{
Test t1(10, 20);
Test t2(100, 200);
//Test t3 = TestAdd(t1, t2);
Test t3 = t1.TestAdd(t2);
t3.printT();
//((t1 += t2) += t2 )+= t2
//如果相对一个对象连续调用成员方法,每次都会改变对象本身,成员方法需要返回引用。
t1.TestAdd2(t2).TestAdd2(t2);
t1.printT();
return 0;
}
自定义数组类
main.cpp
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include "MyArray.h"
using namespace std;
int main(void)
{
MyArray array1(10);//开辟10元素的数组
//赋值操作
for (int i = 0; i < 10; i++) {
array1.setData(i, i + 10);
}
cout << "--------" << endl;
cout << "array1:" << endl;
for (int i = 0; i < 10; i++) {
cout << array1.getData(i) << " ";
}
cout << endl;
MyArray array2 = array1;
cout << "array2:" << endl;
for (int i = 0; i < array2.getLen(); i++) {
cout << array2.getData(i) << " ";
}
cout << endl;
MyArray array3;
array3 = array1;
cout << "array3:" << endl;
for (int i = 0; i < array3.getLen(); i++) {
cout << array3.getData(i) << " ";
}
cout << endl;
return 0;
}
Array.h
#pragma once
#include <iostream>
using namespace std;
class MyArray
{
public:
MyArray();
MyArray(int len);
MyArray(const MyArray &another);
~MyArray();
void setData(int index, int data);
int getData(int index);
int getLen();
void operator=(const MyArray& another);
private:
int len;
int *space;
};
Array.cpp
#include "MyArray.h"
MyArray::MyArray()
{
cout << "MyArray()..." << endl;
this->len = 0;
this->space = NULL;
}
MyArray::MyArray(int len)
{
if (len <= 0) {
this->len = 0;
return;
}
else {
this->len = len;
//给space开辟空间
this->space = new int[this->len];
cout << "MyArray::MyArray(int len) ..." << endl;
}
}
MyArray::MyArray(const MyArray &another)
{
if (another.len >= 0) {
this->len = another.len;
//深拷贝
this->space = new int[this->len];
for (int i = 0; i < this->len; i++) {
this->space[i] = another.space[i];
}
cout << "MyArray::MyArray(const MyArray &another) ..." << endl;
}
}
MyArray::~MyArray()
{
if (this->space != NULL) {
delete[]this->space;
this->space = NULL;
len = 0;
cout << "MyArray::~MyArray() ..." << endl;
}
}
void MyArray::setData(int index, int data)
{
if (this->space != NULL) {
this->space[index] = data;
}
}
int MyArray::getData(int index)
{
return this->space[index];
}
int MyArray::getLen()
{
return this->len;
}
void MyArray::operator=(const MyArray& another)
{
if (another.len >= 0) {
this->len = another.len;
//深拷贝
this->space = new int[this->len];
for (int i = 0; i < this->len; i++) {
this->space[i] = another.space[i];
}
cout << "MyArray::operator=(const MyArray& another) ..." << endl;
}
}
c++-变量,this指针,全局函数,成员函数,自定义数组类的更多相关文章
- this指针和const成员函数
1.this指针 1)this指针是成员函数的隐式参数,成员函数通过this指针来访问调用它自己的那个对象,成员函数对各成员的访问均通过this进行 2)当某个对象调用它的成员函数时,编译器会把这个对 ...
- c++全局函数 && 成员函数
#include<iostream> using namespace std; class Test { public: Test(, ) { this->a = a; this-& ...
- this指针与const成员函数
this指针的类型为:classType *const // 即指向类类型非常量版本的常量指针 所以,我们不能把this绑定到一个常量对象上 ===> 不能在一个常量对象上调用普通的 ...
- 为什么NULL指针也能访问成员函数?(但不能访问成员变量)
查看更加详细的解析请参考这篇文章:http://blog.51cto.com/9291927/2148695 看一个静态绑定的例子: 1 #include <iostream> 2 3 u ...
- C++基础 (4) 第四天 this指针 全局函数和成员函数 友元 操作符重载
1static强化练习-仓库进货和出货 #define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; c ...
- C++类内存布局图(成员函数和成员变量分开讨论)
一.成员函数 成员函数可以被看作是类作用域的全局函数,不在对象分配的空间里,只有虚函数才会在类对象里有一个指针,存放虚函数的地址等相关信息. 成员函数的地址,编译期就已确定,并静态绑定或动态的绑定在对 ...
- 成员函数指针与高性能C++委托
1 引子 标准C++中没有真正的面向对象的函数指针.这一点对C++来说是不幸的,因为面向对象的指针(也叫做“闭包(closure)”或“委托(delegate)”)在一些语言中已经证明了它宝贵的价值. ...
- [转]成员函数指针与高性能的C++委托
原文(作者:Don Clugston):Member Function Pointers and the Fastest Possible C++ Delegates 译文(作者:周翔): 成员函数指 ...
- 第24课.经典问题解析(1.析构函数的顺序;2.const修饰对象;3.成员函数,成员变量是否属于具体对象)
1.当程序中存在多个对象的时候,如何确定这些对象的析构顺序? 单个对象 单个对象创建时构造函数的调用顺序 a.调用父类的构造函数 b.调用成员变量的构造函数(调用顺序与声明顺序相同) c.调用类自身的 ...
随机推荐
- Linux下为知笔记和蚂蚁笔记测评,推荐蚂蚁笔记!(非广告)
本人由于学习Linux,需要一款可以在Linux平台下可以运行的一款软件,了解到为知笔记之笔记(下文以W代替)和蚂蚁笔记(下文以M代替)比较出名,由于某云和某象笔记在linux平台下没有对应的软件,所 ...
- 2019年12月1日Linux开发手记
配置ubuntu摄像头: 1.设置→添加→usb控制器→兼容usb3.0 2.虚拟机→可移动设备→web camera→连接(断开主机) 3.查看是否配置成功,打开终端,输入: susb ls /de ...
- HttpClientFactory 日志不好用,自己扩展一个?
前言 .NetCore2.1新推出HttpClientFactory工厂类, 替代了早期的HttpClient, 并新增了弹性Http调用机制 (集成Policy组件). 替换的初衷还是简单说下: ① ...
- 【数据结构】之链表(C语言描述)
链表是线性表的一种,是一种物理存储单元上非连续的存储结构,链表中的数据元素之间是通过指针链接实现的. 链表由一系列节点组成,节点可以在运行时动态的生成. 链表中国的每个节点分为两部分:一部分是存储数据 ...
- Docker harbor 安装和基础操作
目录 简介 离线安装 配置文件 安装 查看 访问测试 及 简单操作 创建一个用户 创建一个测试仓库 创建测试仓库 测试上传和下载镜像 上传 下载镜像测试 简介 Docker容器应用的开发和运行离不开可 ...
- 区块链学习笔记:DAY05 如何使用公有云区块链服务
这是最后一节课了,主要讲华为云在云区块链提供的服务,如何基于华为云BCS来构建应用 先来个简单的比喻: 1.有关BaaS的范围定义 包含物理主机.虚拟主机.容器服务.区块链.智能合约和服务 2.华为云 ...
- Oracle_视图_索引_plsql_游标_存储过程_存储函数_触发器
-- 感觉有用点个赞呗^v^ select * from emp; drop view persin_vw;--删除视图 create table emp as select * from scott ...
- 浅议Grpc传输机制和WCF中的回调机制的代码迁移
浅议Grpc传输机制和WCF中的回调机制的代码迁移 一.引子 如您所知,gRPC是目前比较常见的rpc框架,可以方便的作为服务与服务之间的通信基础设施,为构建微服务体系提供非常强有力的支持. 而基于. ...
- 第六章 jQuery选择器
jQuery选择器概述: 选择器jQuery基础,在jQuery中,对事件处理,遍历DOM和Ajax操作都依赖于选择器. 什么是jQuery选择器: jQuery选择器拥有良好的浏览器兼容性,不用使用 ...
- Oracle - 给rac创建单实例dg,并做主从切换
一.概述 本文将介绍如何给rac搭建单节点的dg,以及如何对其进行角色转换.预先具备的知识(rac搭建,单实例-单实例dg搭建) 二.实验环境介绍 主库rac(已安装rac,并已有数据库orcl)ra ...