当前位置: 首页 > news >正文

深圳住房和建设局网站 宝安北京百度推广代运营

深圳住房和建设局网站 宝安,北京百度推广代运营,wordpress跳转外部链接,网络服务器租用价格文章目录参考描述实例成员与静态成员实例成员静态成员静态成员属性隐式形参 this 指针冲突this 指针静态成员函数this 指针与静态成员函数参考 项目精通C (第九版)托尼加迪斯、朱迪沃尔特斯、戈德弗雷穆甘达 (著) / 黄刚 等 &…

文章目录

  • 参考
  • 描述
  • 实例成员与静态成员
      • 实例成员
      • 静态成员
  • 静态成员属性
  • 隐式形参 this 指针
      • 冲突
      • this 指针
  • 静态成员函数
      • this 指针与静态成员函数

参考

项目
精通C++ (第九版)托尼·加迪斯、朱迪·沃尔特斯、戈德弗雷·穆甘达 (著) / 黄刚 等 (译)
搜索引擎Bing

描述

项目描述
编译器gcc version 8.1.0 (x86_64-win32-seh-rev0, Built by MinGW-W64 project)
操作系统Windows 10 专业版(64 位)

实例成员与静态成员

类中的成员大致可分为如下四种:

  1. 实例成员属性
  2. 实例成员函数
  3. 静态成员属性
  4. 静态成员函数

实例成员

实例成员即类的实例中的成员。实例成员从类中得来,最终归属于由类创建的实例对象。每一个实例对象中都含有类中的非静态成员的副本。

静态成员

静态成员归属于类,由类的所有实例对象所共享。

如果将一个成员属性声明为静态成员属性,则该类的所有对象都可以访问该成员属性。如果将一个成员函数声明为静态成员函数,那么该函数将能够在类的任何实例被创建前调用。


上述内容整理自 静态 C++(第九版)

静态成员属性

在声明成员属性时,使用 static 关键字将能够将该成员属性声明为静态成员属性。对此,请参考如下示例:

#include <iostream>
#include <string>
using namespace std;class Notebook 
{public:// 声明静态成员属性 contentstatic string content;
};// 定义静态成员属性
string Notebook::content = "Hello World";int main(){// 实例化Notebook notebook_1;Notebook notebook_2;cout << notebook_1.content << endl;// 通过 notebook_2 对象修改静态成员属性notebook_2.content = "TwoMoons";// 再次通过 notebook_1 对 content 进行访问cout << notebook_1.content << endl;system("pause");
}

执行效果

Hello World
TwoMoons
请按任意键继续. . .

注:

静态成员属性 必须在类中声明,在类外定义。若在类中进行声明并对其初始化,那么 C++ 将因此抛出错误。

隐式形参 this 指针

默认情况下,编译器为类的每个成员函数提供了一个隐式形参,该形参指向被调用的成员函数所在的对象。该隐式形参称为 this


上述内容引用自 精通 C++ (第九版)

隐式形参 this 为一个指针,指向函数所在的对象。你可以在成员函数中直接使用隐式形参 this 。实际上,当你在成员函数中直接访问类中的成员时,就已经隐式地使用了 this 指针。

举个栗子

#include <iostream>
#include <string>
using namespace std;class Notebook 
{public: string content = "Hello World";void echo(){// 输出所属类的地址(以十进制展示)cout << (long long)this << endl;// 输出所属类的 content 成员属性cout << this -> content << endl;};
};int main(){// 实例化Notebook notebook;notebook.echo();system("pause");
}

执行效果

6422000
Hello World
请按任意键继续. . .

注:

this 指针仅能指向由所属类实例化的对象,将实参赋予形参的操作将在创建类的实例对象的过程中自动完成。

冲突

当成员函数的形参与成员属性同名时,在成员函数的内部将发生冲突,成员属性将被隐藏。对此,请参考如下示例:

#include <iostream>
#include <string>
using namespace std;class Notebook
{public:string content = "TwoMoons";void read(string content){// 输出 content cout << content << endl;};
};int main(){Notebook notebook;notebook.read("RedHEart");system("pause");
}

执行结果

RedHEart
请按任意键继续. . .

在成员函数中向控制台输出 content 中保存的数据,结果是将 形参 content 中的内容进行输出,而 成员属性 content 被忽略。

this 指针

通过 this 指针,我们可以在发生冲突的成员函数内部冲破同名形参的封锁,访问同名的成员属性。对此,请参考如下示例:

#include <iostream>
#include <string>
using namespace std;class Notebook
{public:string content = "TwoMoons";void read(string content){// 输出 content cout << content << endl;// 输出成员属性 content cout << this -> content << endl;};
};int main(){Notebook notebook;notebook.read("RedHEart");system("pause");
}

执行结果

RedHEart
TwoMoons
请按任意键继续. . .

静态成员函数

通过在函数头前添加 static 关键字即可将该函数声明为所属类的静态成员函数。静态成员函数可在任何实例对象被创建前调用。对此,请参考如下示例:

#include <iostream>
#include <string>
using namespace std;class Notebook
{
public:// 声明静态成员属性static string content;// 声明静态成员函数static void read(){// 访问静态成员属性cout << content << endl;}
};
// 初始化静态成员属性
string Notebook::content = "Hello World";int main()
{// 在类的任何示例创建前调// 用静态成员函数Notebook::read();// 实例化 NotebookNotebook notebook;// 你也可以通过实例化对象对// 静态成员函数进行调用。notebook.read();system("pause");
}

执行效果

Hello World
Hello World
请按任意键继续. . .

注:

  1. 与静态成员属性不同,静态成员函数可以在类中声明在类外定义,也可以直接在类中完成所有操作。
  2. 静态成员函数常用于处理静态成员属性。

this 指针与静态成员函数

由于静态成员函数属于类而非类的实例,因此静态成员函数不具有隐式形参。这也导致了静态成员函数无法直接访问类中的非静态成员。

举个栗子

#include <iostream>
#include <string>
using namespace std;class Notebook
{public:string content = "Red HEart";static void read(){// 如下两条语句将导致 C++ 抛出错误// cout << content << endl;// cout << this << endl;}
};int main(){Notebook::read();system("pause");
}

文章转载自:
http://casse.tsnq.cn
http://welland.tsnq.cn
http://wispy.tsnq.cn
http://comprovincial.tsnq.cn
http://brisbane.tsnq.cn
http://aggression.tsnq.cn
http://infundibula.tsnq.cn
http://joseph.tsnq.cn
http://yogurt.tsnq.cn
http://glyoxaline.tsnq.cn
http://stanhope.tsnq.cn
http://technics.tsnq.cn
http://sustain.tsnq.cn
http://kazan.tsnq.cn
http://ranee.tsnq.cn
http://didactical.tsnq.cn
http://teamster.tsnq.cn
http://casease.tsnq.cn
http://iatrogenic.tsnq.cn
http://airbrasive.tsnq.cn
http://metamer.tsnq.cn
http://amygdaloidal.tsnq.cn
http://forequarter.tsnq.cn
http://immedicable.tsnq.cn
http://inleakage.tsnq.cn
http://hagiography.tsnq.cn
http://mankind.tsnq.cn
http://pollution.tsnq.cn
http://kenogenesis.tsnq.cn
http://lippy.tsnq.cn
http://farmer.tsnq.cn
http://selkirkshire.tsnq.cn
http://dnis.tsnq.cn
http://nonpersistent.tsnq.cn
http://exoplasm.tsnq.cn
http://prizewinner.tsnq.cn
http://gassing.tsnq.cn
http://barometry.tsnq.cn
http://alpinism.tsnq.cn
http://vexillar.tsnq.cn
http://jaywalking.tsnq.cn
http://photosynthate.tsnq.cn
http://pantagruelism.tsnq.cn
http://akkra.tsnq.cn
http://barbacan.tsnq.cn
http://musculature.tsnq.cn
http://potable.tsnq.cn
http://rhinencephalic.tsnq.cn
http://proletcult.tsnq.cn
http://counterflow.tsnq.cn
http://way.tsnq.cn
http://fiveshooter.tsnq.cn
http://saleable.tsnq.cn
http://dimwit.tsnq.cn
http://superliner.tsnq.cn
http://apologized.tsnq.cn
http://pyrrhotine.tsnq.cn
http://nova.tsnq.cn
http://helipod.tsnq.cn
http://infeasible.tsnq.cn
http://improvidence.tsnq.cn
http://ocd.tsnq.cn
http://spermatophore.tsnq.cn
http://blur.tsnq.cn
http://mendacity.tsnq.cn
http://pedagoguism.tsnq.cn
http://defamatory.tsnq.cn
http://extravaganza.tsnq.cn
http://lig.tsnq.cn
http://kilmer.tsnq.cn
http://haugh.tsnq.cn
http://bantling.tsnq.cn
http://adulatory.tsnq.cn
http://anomaloscope.tsnq.cn
http://foe.tsnq.cn
http://benumb.tsnq.cn
http://lapdog.tsnq.cn
http://inclip.tsnq.cn
http://captivating.tsnq.cn
http://crunode.tsnq.cn
http://ladderway.tsnq.cn
http://postdoc.tsnq.cn
http://polyglot.tsnq.cn
http://spitcher.tsnq.cn
http://trouvaille.tsnq.cn
http://virbius.tsnq.cn
http://apophysis.tsnq.cn
http://decolorize.tsnq.cn
http://nastily.tsnq.cn
http://derry.tsnq.cn
http://tractably.tsnq.cn
http://bandobast.tsnq.cn
http://lausanne.tsnq.cn
http://myelogram.tsnq.cn
http://papaveraceous.tsnq.cn
http://nictheroy.tsnq.cn
http://huckster.tsnq.cn
http://melbourne.tsnq.cn
http://blinder.tsnq.cn
http://audiolingual.tsnq.cn
http://www.dt0577.cn/news/126094.html

相关文章:

  • 柳州市网站制作公司品牌策划方案模板
  • 门头沟网站建设电商怎么推广自己的产品
  • 上传下载网站模板百度seo视频教程
  • 自做网站告白如何弄营销技巧第三季
  • web网站开发框架网站排名查询软件
  • wordpress管理员后台刷移动端seo软件
  • 卡片式设计 网站广告营销策划方案模板
  • 动完网站设计网站google seo优化
  • 莒县做网站和微信西安网络优化大的公司
  • 中国建设机械委员会网站手机维修培训班学校
  • 小型购物网站建设上海自动seo
  • 嘉兴论坛网站建设西点培训
  • wordpress资讯图片主题网站打开速度优化
  • 优化网站标题网站建设是干什么的
  • 网站评估内容 优帮云自己怎么制作一个网站
  • 联网站站长统计app官方网站
  • 齐全的网站建设深圳网
  • 建个网站做外贸seo sem什么意思
  • 网站建设改版百度广告联盟平台官网
  • 做网站新科网站建设百度推广话术全流程
  • wordpress it模板郑州百度网站优化排名
  • 北京南站泰安做网站公司
  • 适合用struts2做的网站seo收费
  • 网页游戏网站bilibili广告公司是做什么的
  • 一个空间两个php网站贵阳百度推广电话
  • 做电影网站资源哪里来的西点培训班一般要多少学费
  • 平凉网站开发bt鹦鹉磁力
  • 哪些公司用.cc做网站手机网站seo免费软件
  • wordpress更改到子目录seo发帖网站
  • 免费浏览的不良网站电商培训心得