#include <iostream>
using namespace std;
/************************************************************************
由于成员函数不占用对象空间大小,非静态成员函数只会诞生一份函数实例,多个同类型对象共用一块代码
如何区分哪个对象调用自己的呢
this指针隐含在每一个非静态成员函数内,指向被调用成员函数所属的对象
this指针用途
1.当形参和数据成员变量同名时,可以使用this指针来区分;
2.在类的非静态成员函数返回对象本身,可以使用return *this
/************************************************************************/
class Person{
public:
Person(int age)
{
//age = age; //发现输出是一个未初始化的未知值
this->age = age; //this指针指向的是被调用成员函数所属对象
}
int get_value()
{
return age;
}
//返回引用
Person& get_person_age(const Person &per)
{
this->age += per.age;
return *this;//this是指向被调用成员函数所属对象,*this是被调用成员函数对象本身
}
//返回对象
Person get_person_age1(const Person &per)
{
this->age += per.age;
return *this;//返回的是一个对象
}
private:
int age;
};
void test1()
{
Person per(10);
cout<<"age:"<<per.get_value()<<endl;
Person per1(10);
//链式编程
per1.get_person_age(per).get_person_age(per).get_person_age(per);
cout<<"age:"<<per1.get_value()<<endl;//输出40
Person per2(10);
per2.get_person_age1(per).get_person_age1(per).get_person_age1(per);
cout<<"age:"<<per2.get_value()<<endl;//输出20,原因是get_person_age1返回一个对象,该对象和per2是没有关系的,除了第一次per2对象值发生改变
Person per3(10);
cout<<"age:"<<per3.get_person_age1(per).get_person_age1(per).get_person_age1(per).get_value()<<endl;//输出40
}
int main()
{
test1();
return 0;
}
相关推荐
实验六 存储器实验
只读存储器(ROM)是一种在正常工作时其存储的数据固定不变,其中的数据只能读出,不能写入,即使断电也能够保留数据,要想在只读存储器中存入或改变数据,必须具备特定的条件。当地址为00000101时,会分别在数据位宽为8,16,32的只读寄存器中找到地址为00000101所对应的十进制地址位置,也就是地址为5的数据,并将其分别以8位,16位,32位输出。因此,输出保持为0。因此,当异步清零信号为1时,无论其他输入信号是什么,触发器的输出都会被强制清零,输出为0。1)寄存器输入0011,使能端1,异步清零0。
类中非静态成员函数中的----this指针
首先思考一下在c++中,编译器是如何识别一个类的? 识别一个类可以分为一下三步? ①识别类名; ②识别数据成员; ③识别成员函数并将其改写。 引出的问题:类中的数据成员相当于全局变量,但是全局变量在使用之前,必须先定义,而类中的数据成员的位置可以任意,这就是因为识别类时的顺序而导致的区别。我们都知道类中成员函数和数据成员的存储方式: 类的大小包括非静态数据成员而不包括成员函数
AMEsim柱塞泵的建模学习
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录前言一、为什么要对柱塞泵进行仿真?二、Amesim中柱塞泵的模型1.amesim中自己的定排量五柱塞的斜盘柱塞泵2.压力、流量仿真总结 前言 最近在学习Amesim进行柱塞泵的仿真学习,顺便在这里记录我的学习过程,这是我的初衷 提示:以下是本篇文章正文内容,下面案例可供参考 一、为什么要对柱塞泵进行仿真? 柱塞泵是功能和动力学比较复杂的流体机械。而在实际实验中,往往没有条件或者需要对柱塞泵做一些优化,所以需要我们利用仿真...
C++中为什么存在this指针?
在这篇文章中 初识类和对象,谈到了类的初步认识和基本操作方法。 成员函数存储在公共的代码段,不同的对象调用成员函数时候,那么编译器如何知道具体操作的是哪一个对象呢? 在这里就引出了this指针。 C++编译器给每个 “非静态的成员函数” 增加了一个隐藏的指针参数,让该指针指向当前对象(函数运行时调用该函数的对象),在函数体中所有成员变量的操作,都是通过这个指针来完成的。只不过所有的操作对用户来说都是透明的,不需要用户来传递,编译器自动完成。 this指针特性: 只
蓝桥杯单片机学习13——NE555方波发生器&频率测量
NE555详解、引脚图介绍、原理图及工作原理讲解、方波产生的原理、定时器测量频率的原理及使用方法、代码实现
WKWebView 白屏问题,WKWebView request body丢失问题
1,WKWebView 白屏问题 WKWebView 自诩拥有更快的加载速度,更低的内存占用,但实际上 WKWebView 是一个多进程组件,Network Loading 以及 UI Rendering 在其它进程中执行。初次适配 WKWebView 的时候,我们也惊讶于打开 WKWebView 后,App 进程内存消耗反而大幅下降,但是仔细观察会发现,Other Process 的内存占用会...
C++中this指针详解(写的不错)
this指针 在建立对象时,系统会为每一个对象分配独立的存储空间,也就是给每个对象中的数据成员都分配有自己独立的存储空间。如果对同一个类定义n个对象,则有n组同样大小的空间以存放对象中的数据成员。但对于成员函数来说,对象中的数据成员。一个函数的代码段在内存中只有一份。也就是说,函数的代码段在内存中只有一份。也就是说,同一个类中的不同对象在调用自己的成员函数时,一个类中的不同对象在调用自己的成
VxWorks系列 快速导航
Vxworks系列快速阅览导航
C++基础(二)C/C++中的静态、非静态成员函数、变量
C++中每个对象都有自己独立的空间,但是C++的类成员函数只有一份,为全部类对象使用,每个成员函数(非静态)的都有个隐含参数,即this指针,用了接收对象的地址,看起来是对象调用成员函数,事实是成员函数以this为参数,进行数据操作; 这里涉及的成员函数分为静态成员函数,静态成员变量,以及非静态成员函数; 先说C中静态变量以及静态函数: 在C语言中,一个标识符具有三种属性:存储属性,链接属性
ScanNetV2 数据集讲解和选择性下载
写在前面的话: 不可转载!!!!!!!!!!!! ScanNetV2一共1.2T,数据太大本文会简单讲解如何下载自己想要的文件,并附上修改后的下载程序 ScanNetV2:http://www.scan-net.org/ 简介 ScanNetV2的作者是斯坦福大学的博士,其团队通过收集RGB-D的视频序列,通过ipad应用加深传感器而收集的,然后视频会被上传到服务器,并被自动重建。然后,视频会被给到亚马逊 Mechanical Turk,将标注工作众包出去。ScanNetV2是RGB-D数据集,.
2026年数字IC设计华为笔试带答案解析
这篇文章是一份数字IC设计工程师笔试的试卷结构和部分题目解析。试卷包含单选题31题(每题2分)、多选题10题(每题4分)和简答/编程题4题(每题7分),总分130分,考试时长120分钟。 摘要重点: 试卷涵盖数字IC设计的关键知识点,包括Verilog语法、时序逻辑、状态机设计、跨时钟域处理等; 题目解析详细说明了各选项的正确性,如非阻塞赋值特性(D错误)、功耗影响因素(C与组合逻辑级数弱相关)等; 包含实际工程问题,如DDR4接口电平(SSTL-1.2)、卡诺图变量排序(格雷码)等; 部分题目考察基础概念
详解C++中this指针
1、简述 类的所有非静态成员函数都有一个隐含参数,即this指针。在创建一个类的对象时,this指针就被初始化指向该对象。当一个对象调用其成员函数时,this指针作为一个隐含参数,被赋值为当前对象的地址,即this指针指向了当前的调用对象。 2、this指针的用处 一个对象的this指针并不是对象本身的一部分,不会影响sizeof(对象)的结果。this作用域是在类内部,当在类的非静态成员函...
《Python星球日记》 第72天:问答系统与信息检索
《Python星球日记》 第72天:问答系统与信息检索,在前面的日子里,我们学习了自然语言处理的基础知识,包括文本预处理、词向量、Transformer架构和各种预训练模型。今天,我们将探索问答系统这一NLP的重要应用,以及如何结合信息检索技术构建智能问答系统。
this 指针:指向对象自身的隐含指针
this指针(this pointer),是C++编译器自动隐含在所有非静态成员函数指向当前正在调用该非静态成员函数的对象,即“对象自身的指针”。我们可以把this指针理解为:每个非静态成员函数都有一个“隐藏的第一个参数”,这个参数就是this指针,它由编译器自动传递,不需要我们手动声明、也不需要我们手动传递——我们看不见它,但它一直存在,默默发挥作用。this指针的核心是“隐含在非静态成员函数中的、指向当前对象的常量指针”——它看不见、摸不着,但无处不在,是连接非静态成员函数与对象自身的“桥梁”。
蓝牙模块HM-10手册
蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。
jre9下载 jdk9
(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】
this只能用于非静态成员函数内部_C++类的数据和成员函数内存模型(3)
大家都知道,C++是基于C语言之上的语言,在讨论C++数据和成员函数内存布局时,我们先来讨论一下C语言的内存模型。1.C语言数据在内存中的存储位置从程序到a.out 把程序变成.text .data .bss 是编译原理完成的过程。从a.out把程序映射到对应的内存地址空间是操作系统完成的,也就是在操作系统创建进程的时候完成的,在描述进程的那个结构体中。我们常说的堆是为了申请动态内...
深度学习工具包 DeepLearningToolbox
深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[](https://bitdeli.com/free "Bitdeli Badge")
【C++】类和对象--this指针详解
this指针详解
华为的C语言编程规范.pdf
华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。
【C++】this指针
一个对象的this指针并不是对象本身的一部分,不会影响sizeof (对象)的结果。this 作用域是在类内部,当在类的非静态成员函数中访问类的非静态成员的时候,编译器会自动将对象本身的地址作为一个隐含参数传递给函数。 this是指向当前对象的指针,哪个对象调用包含this指针的函数,this指向哪个对象。 this 一般在构造函数中使用,用来区分成员变量和参数。
C++【this指针】总结
介绍了this指针为何引入、this指针的特性和有关this指针的问题:1. this指针能否改变;2. this指针在哪里使用;3. this指针存放在哪里?4. this指针可以为空吗?5. this指针出现的场景
类中函数的一个隐含参数--this指针
为什么会有this指针呢? 我们需要来看一段代码感受一下。 #include <iostream> using namespace std; class Student { public: void InitStudent(char *name,char*gender,int age) { strcpy(_name,na...
浅析this指针
this指针是一个自引用指针,通常用于指向对象自身。本文将深度探讨this指针的核心特性、注意要素、应用场景和相关案例。
this只能用于非静态成员函数内部_this小题
下列关于this指针的叙述中,正确的是( D )。 A) this指针是一个隐含指针,它隐含于类的成员函数中B) 只有在使用this时,系统才会将对象的地址赋值给thisC) 类的友元函数也有this指针D) this指针表示了成员函数当前操作的数据所属的对象 解析] this指针是成员函数所属对象的指针,它指向类对象的地址;它隐含于每个类的非静态成员函数中;thi...
C++This指针的本质
C++This指针的本质前言1.问题2.补充3.解答总结 前言 文章讲述的是C++This指针的本质,偏底层,得懂点汇编的知识,当然,如果你不懂也没关系,我会适当地补充。 1.问题 #include <iostream> #include <string> using namespace std; class CNullPointCall { public: static void Test1(); void Test2(); void Test
this指针
this是一个指针,它时时刻刻指向你这个实例本身。你可以看到它的成员函数、成员变量,this指针本质上是“成员函数”的形参。你可以看见桌子、椅子、地板等,静态成员函数中不能使用this指针。但是房子你是看不到全貌了。对于一个类的实例来说,当你进入一个房子后,不能给this指针赋值。
this指针原理及使用
当一个对象被创建后,它的每一个成员函数都含有一个系统自动生成的隐含指针this,用以保存这个对象的地址,也就是说虽然我们没有写上this指针,编译器在编译的时候也是会加上的。在c语言中,“分开来声明的,也就是说,语言本身并没有支持“数据”和“函数”之间的关联性我们把这种程序方法称为“程序性的”,由一组“分布在各个以功能为导航的函数中”的算法驱动,它们处理的是共同的外部数据。this指针是C++实现封装的一种机制,它将对象和该对象调用的成员函数连接在一起,在外部看来,每一个对象都拥有自己的函数成员。
C++中的this指针
this指针的概念: this指针是类的指针,指向对象的首地址。 this实际上是成员函数的一个形参,在调用成员函数时将对象的地址作为实参传递给this。所以this指针只能在成员函数中使用,在静态成员函数中不能用this。 this指针只有在成员函数中才有定义,且存储位置会因编译器不同有不同存储位置。 在 C++ 中,每一个对象都能通过this指针来访问自己的地址。this指针是所...
3008



被折叠的 条评论
为什么被折叠?



