类成员函数解析

36、C++ 类成员函数与指针的深入解析 本文深入解析了C++中类成员函数与指针的核心概念,包括this指针的作用、拷贝构造函数的意义与实现、成员运算符的重载方法、静态与非静态成员函数的区别,以及指针到成员运算符的使用。通过具体代码示例和详细解释,帮助开发者更好地掌握面向对象编程中的关键知识点,从而编写高效、稳定的C++程序。 阅读详情

 

1、  构造函数:

(1)      定义:是一个特殊的成员函数,名字与类名相同,创建类类型对象时,由编译器自动调用,在对象的生命周期内只且只调用一次,以保证每个数据成员都有一个合适的初始值。

(2)      特性:

1、函数名与类名相同。

2、没有返回值。

3、有初始化列表(可以不用)。

4、新对象被创建,由编译器自动调用,且在对象的生命期内仅调用一次。

5、构造函数可以重载,实参决定了调用那个构造函数。

6、如果没有显式定义时,编译器会提供一个默认的构造函数。

7、无参构造函数和带有缺省值得构造函数都认为是缺省构造函数,并且缺省构造函数只能有一个。

(3)分类:

 

无参构造函数:如果没有显式的定义一个构造函数,系统将默认合成一个无参构造函数,参数为空,什么都不做。

 

显式的定义一个无参构造函数,如下例:

    Time()

    {

       cout<<"Time(int intint )"<<endl;

    }

 

普通构造函数(也称重载构造函数):

class Time

{

public:

    Time(int h, int m, int s)

    {

       _hour = h;

       _minute = m;

       _second = s;

       cout<<”Time(int, int, int)”<<endl;

    }

private:

    int _hour;

    int _minute;

    int _second;

};

 

 

系统默认合成的构造函数:在A类有缺省的构造函数,B类没有显示的定义构造函数,B类中含有A类类型成员,此时系统默认合成构造函数。

class Time

{

public:

 

    Time()

    {

       cout<<"Time(int intint )"<<endl;

    }

private:

    int _hour;

    int _minute;

    int _second;

};

class Date

{

private:

    int _year;

    int _month;

    int _day;

    Time t;

 

};

 

 

运行结果:

 

 

 

 

初始化列表:

以一个冒号开始,接着是一个以逗号分隔的数据成员列表,每个数据成员后面跟一个放在园括号中的初始化式。

classDate

{

public:

     Date(int year = 1, int month = 2, int day = 3)

       :_year(year),

       _month(month),

       _day(day)

       {

           cout<<"Date(int, int, int)"<<endl;

       }

private:

    int _year;

    int _month;

    int _day;

 

};

初始化顺序:

 

1、初始化列表仅用于初始化数据成员,并不指定这些数据成员的初始化顺序,数据成员在类中定义顺序就是在参数列表中的初始化顺序。

 

2、每个成员在初始化列表中只能出现一次,尽量避免使用成员初始化成员,成员的初始化顺序最好和成员的定义顺序保持一致。

 

类中包含以下成员必须要放在初始化列表中初始化:

class Time

{

public:

    //Time()

    //{}

    Time(int h, int m, int s)

    {

       cout<<"Time(int intint )"<<endl;

    }

private:

    int _hour;

    int _minute;

    int _second;

};

 

class Date

{

public:

     Date(int year, int month, int day)

       :_year(year),

       _month(month),

       _day(day),

       t(0, 0, 0)

       {

           cout<<_year<<"-"<<_month<<"-"<<_day<<endl;

       }

private:

    int _year;

    int _month;

    int _day;

    const int a;

    int& b;

};

1、引用数据成员

2、const数据成员

 



 

3、类类型成员(该类没有缺省的构造函数):Time类中没有缺省的构造函数(无参构造函数,有参数,但有缺省的构造函数),要调用Date类创建对象,则类内的成员都要初始化,Date类中含有Time类类型成员,只能在初始化列表内进行初始化

 


 

 

 

 

 

2、  拷贝构造函数

显式的定义拷贝构造函数:

class Date

{

public:

     Date(int year = 1, int month = 2, int day = 3)

       :_year(year),

       _month(month),

       _day(day)

       {

           cout<<"Date(int, int,int)"<<endl;

       }

     Date(const Date &d)

       :_year(d._year),

       _month(d._month),

       _day(d._day)

     {

       cout<<"Date(&d)"<<endl;

     }

 

private:

    int _year;

    int _month;

    int _day;

};

void FunTest()

{

    Date d;

    Date d1(d);

}

运行结果:

       系统默认合成拷贝构造函数:在A类有缺省的构造函数,B类没有显示的定义拷贝构造函数,B类中含有A类类型成员,此时系统默认合成拷贝构造函数。

class Time

{

public:

 

    Time()

    {

       cout<<"Time(int intint )"<<endl;

    }

private:

    int _hour;

    int _minute;

    int _second;

};

class Date

{

public:

     Date(int year = 1, int month = 2, int day = 3)

       :_year(year),

       _month(month),

       _day(day)

       {

           cout<<"Date(int, int,int)"<<endl;

       }

 

private:

    int _year;

    int _month;

    int _day;

    Time t;

};

void FunTest()

{

    Date d;

    Date d1(d);

}

运行结果:

 

3、  析构函数:与构造函数功能相反,在对象被销毁时,由编译器自动调用,完成类的一些资源清理和汕尾工作

 

析构函数特性:

a、析构函数在类名(即构造函数名)加上字符~。

b、析构函数无参数无返回值。

c、一个类有且只有一个析构函数。若未显示定义,系统会自动生成缺省的析构函数。

d、对象生命周期结束时,C++编译系统系统自动调用析构函数。

e、注意析构函数体内并不是删除对象,而是做一些清理工作

 

class Date

{

public:

    Date(int year, int month, int day)

       : _year(year)

       , _month(month)

       , _day(day)

    {

       cout<<"Date(int, int,int):"<<this<<endl;

    }

 

    Date(const Date& d)

       : _year(d._year)

       , _month(d._month)

       , _day(d._day)

    {

       cout<<"Date(constDate& d):"<<this<<endl;

    }

 

    Date& operator=(const Date& d)

    {

       cout<<"Date&operator=(const Date& d):"<<this<<endl;

       if (this != &d)

       {

           _year = d._year;

           _month = d._month;

           _day = d._day;

       }

 

       return *this;

    }

 

    ~Date()

    {

       cout<<"~Date():"<<this<<endl;

    }

 

private:

    int _year;

    int _month;

    int _day;

};

 

void FunTest()

{

    Date d(2016, 10, 17);

    Date d1(d);

}

运行结果:



析构函数销毁对象原则:先构造后析构,后构造先析构。

 

赋值运算符重载:

 

      在面向对象程序设计中,对象间的相互拷贝和赋值是经常进行的操作,如果对象在申明的同时马上进行的初始化操作,则称之为拷贝运算。例如:
       class A(“Date”) ;  class B = A;
     此时其实际调用的是B(A)这样的浅拷贝操作。


    如果对象在申明之后,在进行的赋值运算,我们称之为赋值运算。例如:
        class  A("Date");  class B;
        B=A;
        此时实际调用的类的缺省赋值函数B.operator=(A);

 

class Date

{

public:

    Date(int year = 2016, int month = 10 , int day = 1)

       : _year(year)

       , _month(month)

       , _day(day)

    {

       cout<<"Date(int, int,int):"<<this<<endl;

    }

 

    Date(const Date& d)

       : _year(d._year)

       , _month(d._month)

       , _day(d._day)

    {

       cout<<"Date(constDate& d):"<<this<<endl;

    }

 

    Date& operator=(const Date& d)

    {

       cout<<"Date&operator=(const Date& d):"<<this<<endl;

       if (this != &d)

       {

           _year = d._year;

           _month = d._month;

           _day = d._day;

       }

       return *this;

    }

 

    ~Date()

    {

       cout<<"~Date():"<<this<<endl;

    }

 

private:

    int _year;

    int _month;

    int _day;

};

void FunTest()

{

    Date d(2016, 10, 17);

Date d1(d); // 调用拷贝构造函数进行拷贝

Date d2;

    D2 = d;    //d1.operator(&d)赋值运算符重载

}


程序编译之后,d1 和 d2 在栈上都分配了内存,,对象d1 的域被初始化,d2为随机值。

如果我们简单的执行Date d1(d); 即 Date d1 = d; 则其执行的是缺省定义的缺省的赋值运算。所谓缺省的赋值运算,是指对象中的所有位于stack中的域,进行相应的复制。但是,如果对象有位于heap上的域的话,其不会为拷贝对象分配heap上的空间,而只是指向相同的heap上的同一个地址。


因此,对于缺省的赋值运算,如果对象域内没有heap上的空间,其不会产生任何问题。但是,如果对象域内需要申请heap上的空间,那么在析构对象的时候,就会连续两次释放heap上的同一块内存区域,从而导致异常。

 

故需要用赋值运算符重载

 

Date d2;

    D2 = d;    //d1.operator(&d)赋值运算符重载

 

  Date& operator=(const Date& d)

    {

       cout<<"Date&operator=(const Date& d):"<<this<<endl;

        if (this != &d)

       {

           _year = d._year;

           _month = d._month;

           _day = d._day;

       }

       return *this;

  }//一定要返回引用,否则返回其值后立即消失,不能连续赋值

 




22、C++ 类成员函数、访问级别与面向对象编程解析 本博客深入解析了C++中类的成员函数,包括const成员函数的使用及其重要性,探讨了访问级别的设定(public与private),并通过实例说明如何通过成员函数限制对数据的访问。此外,博客还介绍了面向对象编程的基础概念,如对象的属性与行为、分类与继承结构,并展示了多态性的实现方式。通过这些内容的学习,读者可以更好地掌握C++面向对象编程的核心思想与实践技巧。 阅读详情

相关推荐

C#类成员与构造函数深入解析

本文详细解读了C#中类方法和成员函数的概念,特别是构造函数和析构函数的使用。通过代码示例,阐释了构造函数如何在对象创建时自动执行以及如何控制对象的初始化。同时,也探讨了析构函数的不确定性以及与C++中的析构函数的区别。

weixin_42510201的博客 954

C++程序设计——类的六个成员函数

C++程序设计:类的六个默认成员函数

m0_63020222的博客 4398

C++类成员函数与对象操作深入解析

本文深入探讨了C++中类成员函数的声明与定义、对象作为函数参数、返回对象以及默认复制构造函数的使用。通过代码示例和图示,展示了如何在类外部定义成员函数,如何处理对象作为参数和返回值的情况,以及默认复制构造函数在对象初始化中的作用。理解这些概念对于编写高效且结构清晰的C++程序至关重要。

weixin_42518874的博客 450

类的成员函数总结

本文主要讲解了C++中类的成员函数(构造函数、析构函数、拷贝构造函数)

可涵的博客 1466

LSTM cell结构的理解和计算

LSTM是传统RNN网络的扩展,其核心结构是其cell单元,网上LSTM的相关资料繁多,质量参差不齐,下面主要结合LSTM神经网络的详细推导和 Christopher Olah的blog两篇文章中的内容进行说明。主要介绍网络如何计算,为何这么算先不展开:)。前者一副图加上29个公式,简洁明了;后者娓娓道来,适合初学者。首先是LSTM cell最常见的结构图: 这是变形的版本(找不到更清晰的版本了),

songhk0209的博客 2万+

OpenCV图像去畸变实战指南:从原理到参数调优(附完整代码与效果对比)

本文深入解析了OpenCV图像去畸变的原理与实战技巧,详细对比了标准透视、鱼眼及全能型omnidir三种校正函数的核心参数调优方法。通过分析alpha、balance、centerX/Y及focalScale四大参数对输出效果的影响,并结合完整代码与效果对比图,帮助开发者精准校正镜头畸变,提升计算机视觉任务精度。

weixin_29046611的博客 342

类与对象:类的6个默认成员函数: 构造函数、析构函数、拷贝构造函数、赋值操作符重载、默认拷贝构造与赋值运算符重载的问题、const成员函数、 取地址及const取地址操作符重载

1.类的6个默认成员函数 如果一个类中什么成员都没有,简称为空类。任何一个类在我们不写的情况下,都会自动生成下面6个默认成员函数。 构造函数 析构函数 拷贝构造函数 赋值操作符重载 const成员函数 取地址及const取地址操作符重载 2. 构造函数 2.1 概念 构造函数是一个特殊的成员函数,名字与类名相同,创建类类型对象时由编译器自动调用,保证每个数据成员都有 一个合...

qq_43574794的博客 1113

“挑战杯”大赛“揭榜挂帅”赛道-baseline解析

在这个脚本中,txt文件的保存路径为'yolov7/datasets/Fatigue_driving_detection/txt_labels/',而xml文件的路径为'./label_xml/'。标注图片后的产物是会给每一个图片对应生成一个xml格式的文件,但是如果要用yolo框架去训练模型,输入的数据必须是txt格式,因此这里有一步转换格式的过程。注意看:obs这里有三个文件夹,第一个是关键帧的文件夹A,第二个是标注时输出的位置为文件夹B,第三个是智能标注后和手动标注的合集文件夹C。

m0_62899814的博客 2322

【 C++ 】类和对象(中)—— 类的6个默认成员函数

目录 1、类的6个默认成员函数 2、构造函数 构造函数概念 构造函数特性 3、析构函数 析构函数概念 析构函数特性 4、拷贝构造函数 拷贝构造函数概念 拷贝构造函数特性 5、赋值运算符重载 运算符重载 赋值运算符重载 6、const成员 const修饰类的成员函数 7、取地址及const取地址操作符重载 1、类的6个默认成员函数 ......

bit_zyx的博客 4033

Vivado里LDPC IP核怎么配?手把手教你从6144bit数据到完整参数设置

本文详细介绍了在Vivado中配置LDPC IP核的完整流程,从6144bit数据的参数计算到实际IP核设置。通过分析基本图选择、扩展因子计算和移位参数确定等关键步骤,帮助工程师高效完成LDPC编译码的硬件实现,并提供了时序控制、仿真验证和性能优化的实用技巧。

weixin_30352645的博客 519

FPGA与GPU异构计算:重塑嵌入式AI的算力边界与场景定义

本文深入探讨了FPGA与GPU异构计算如何突破嵌入式AI的算力瓶颈。通过分析工业视觉、智能交通及航天等场景,阐述了FPGA负责高速数据采集与预处理,GPU专注复杂AI推理的协同模式,有效解决了传统单一架构在算力、功耗、接口和实时性上的挑战,为嵌入式AI应用提供了高效能解决方案。

spice的博客 486

类与对象(中) 详细讲解类的六个默认成员函数,认真看一定会有收获

目录前言一、类的六个默认成员函数二、构造函数1.概念2.特性三、析构函数1.概念2.特征四、拷贝构造函数1.概念2.特征五、赋值运算符重载1.运算符重载概念2.实现赋值运算符重载六、const修饰类的成员函数七、取地址及const取地址操作符重载总结前言哈喽,大家好,相信看了我上一篇博客的小伙伴已经对类和对象有了一些初步认识,今天我将着重介绍类里面的默认成员函数,希望想要了解的小伙伴不要走开哦。一、类的六个默认成员函数如果一个类里面什么成员都没有,我们一般会把它简称为空类。但空类里真的什么都没有吗?当然不是

weixin_59371851的博客 1877

YOLOv11改进策略【注意力机制篇】| SE、CBAM、ECA、CA、Swin Transformer等注意力机制全面解析与应用

本文详细介绍了五种主流的注意力机制及其在YOLOv11中的应用。通过合理的模块选择和位置部署,注意力机制能够显著提升目标检测性能。

QQ_778132974的博客 877

C++指向类成员函数的指针详细解析

转自 首先 函数指针是指向一组同类型的函数的指针;而类成员函数我们也可以相似的认为,它是指向同类中同一组类型的成员函数的指针,当然这里的成员函数更准确的讲应该是指非静态的成员函数。前者是直接指向函数地址的,而后者我们从字面上也可以知道 它肯定是跟类和对象有着关系的。 typedef int (*p)(int,int);//定义一个接受两个int型且返回int型变量的函数指针类型 int fun...

janeqi1987的专栏 6382

深度学习工具包 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');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

C++ const 与 static 详解:变量、函数、类成员的全面解析

C++中的const和static关键字是控制变量行为和存储方式的核心工具。const用于声明不可变性,可修饰变量、函数参数和成员函数,保证数据不被意外修改;static则控制存储期和链接性,使局部变量持久化、限制全局变量作用域,并实现类成员的共享。两者结合可定义类级别的编译期常量(static constexpr)。使用时需注意const成员函数不能修改状态、static成员需在类外定义等规则,遵循优先使用constexpr、参数用const&等最佳实践,以提升代码安全性和性能。

保持分享欲 1233

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

【C/C++】解析 类成员函数和this指针

类成员函数 类成员函数属于类本身,在编译期间存在,并且每个类只有一份。this指针的作用: 指向调用该函数的对象,函数中使用类成员都是通过this指针调用 连接对象和成员函数,可以在函数中无感知的使用成员 使用: 可以使用 this->成员名 或者省略 this- 它们与是否定义对象无关,可以通过类名直接访问调用。this指针 类中的非静态成员函数包括构造、析构函数,会有一个默认的隐藏的参数this 指针 它是编译器默认加上的在所有参数之前,类型为当前类的指针 即:类 * const this

努力让自己的能力配得上梦想. 789

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

C++中类成员函数的this指针解析

在ubuntu20.04下借助gdb调试工具,探究this指针的秘密

qq_43651585的博客 576

深入解析 const 关键字:指针、参数、返回值和类成员函数

在 C++ 中是用来修饰内置类型变量,自定义对象,成员函数,返回值,函数参数。使用const有以下好处:编译器的静态检查:const 可以让编译器在编译时进行静态检查,确保不会对被声明为 const 的值进行修改。如果尝试修改 const 值,编译器将产生错误或警告。代码可读性和维护性:使用 const 可以清晰地表达代码的语义,指出某个值不会被修改。可以提高代码的可读性,并且在维护代码时能够更好地理解和推理代码的含义。防止意外修改:通过使用 const,可以避免在代码中意外地修改某个值。特别是当多人协作开

卜及中的博客 4618

C++类成员函数指针解析

类的成员函数指针作为参数传递给其他函数和普通函数指针的传递是不同的 普通函数指针的传递只要在参数声明中声明是相同参数个数、类型和相同返回类型的函数指针,可是传递类成员函数指针用此方法却不能工作。指针是指向一些内存地址的变量,既可以是数据的地址也可以是函数的地址。C++的 成员指针遵从同样的原则。但在类内部没有地址;选择一个类的成员意味着在类中偏移。只有把这个偏移和具体对象的开始地址结合,

踏莎行的博客 1390

C++类成员函数重载:从this指针到继承体系的深度解析

函数重载是C++多态性的基础实现方式之一,它允许在同一作用域内定义多个同名函数,通过参数列表的差异实现不同的功能。其核心原理在于编译器在编译期间根据函数签名(包括参数类型、数量和顺序)进行名称修饰和匹配。这一机制极大地提升了代码的表达能力和复用性,是构建直观API和实现泛型编程的重要技术手段。在面向对象编程中,当重载进入类的范畴,情况变得更加复杂:隐式的this指针成为重载决议的“第零个参数”,const成员函数和引用限定符重载允许根据对象的常量性和值类别提供不同实现,而继承体系下的重写(Override)

weixin_30847865的博客 413

冒号 定义成员函数_C++类成员函数定义方法详解

类成员函数定义与常规函数类似,除特殊情况外,它们有一个包含返回类型(可能为 void)、函数名和形参列表(可能为空)的函数头。执行函数动作的语句包含在一对大括号中,跟在函数头后面。我们在《类对象的创建和使用》一节定义 Circle 类时,已经在类声明本身中定义了它的两个成员函数。当一个类函数定义在类声明中时,它被称为内联函数。内联函数提供了在类声明中包含函数信息的便捷方式,但只能在函数体非常短(通...

weixin_39655689的博客 253

C++编译错误C1057-C1144解析:运算符重载与类成员函数规范

在C++编程中,运算符重载和类成员函数是面向对象编程的核心机制,它们允许开发者自定义类型的行为,提升代码的表达能力。其原理基于C++的函数签名规则和对象模型,通过特定的语法扩展语言的操作符语义。掌握这些机制的技术价值在于能够编写更直观、类型安全的代码,同时遵循语言规范可以避免未定义行为。在实际应用场景中,这些特性广泛用于智能指针、容器类、数学库和资源管理类的实现。然而,不正确的使用常会触发C1057至C1144系列编译错误,例如运算符返回类型不匹配或参数规范错误,这些问题正是C++编译器对代码确定性和安全性

weixin_34318326的博客 506
上一篇: C++ this 指针
下一篇: 复数类Complex
Sharp_UP
博客等级 码龄10年 86粉丝 78原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值