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

建设个人购物网站/艾瑞指数

建设个人购物网站,艾瑞指数,上海网站建设公司招聘,蜜芽免费网站域名C隐式转换与explicit关键字 隐式构造函数 隐含的意思是不会明确告诉你要做什么 隐式转换 C允许编译器对代码执行一次隐式转换&#xff0c;而不需要使用casr强制转换 例1 #include <iostream> #include <string>class Entity { private:std::string m_Name;in…

C++隐式转换与explicit关键字

隐式构造函数

隐含的意思是不会明确告诉你要做什么

隐式转换

C++允许编译器对代码执行一次隐式转换,而不需要使用casr强制转换

例1

#include <iostream>
#include <string>class Entity
{
private:std::string m_Name;int m_Age;
public:Entity(const std::string& name): m_Name(name), m_Age(-1) {}Entity(int age): m_Name("Unknown"), m_Age(age) {}
};
void PrintEntity(const Entity& entity)
{// Printing
}
int main()
{Entity a("Cherno");Entity b(22);Entity c = "Cherno";  // 隐式转换Entity d = 22;PrintEntity(22);PrintEntity("Cherno");  // 不能隐式转换,因为"Cherno"不是std::string, 而是一个char数组std::cin.get();
}

隐式的将22转换成一个Entity,构造出一个Entity
PrintEntity(22)可以
PrintEntity("Cherno")不可以
因为"Cherno"不是一个std::string, 而是一个char数组,所以要进行两次转换,一次从char数组转换成std::string, 然后再从std::string转换成Entity,然而,只允许做一次隐式转换

PrintEntity(std::string("Cherno"));
先做一个显式的转换

PrintEntity(Entity("Cherno"));
或者包含在Entity,因为可以将其隐式地将字符数组转换成std::string

explicit关键字

  • 禁用隐式implicit这个功能
  • explicit关键字放在构造函数前面,如果有一个explicit构造函数,意味着没有implicit转换。如果要使用整数构造Entity对象,就必须显式调用此构造函数

例2

#include <iostream>
#include <string>class Entity
{
private:std::string m_Name;int m_Age;
public:Entity(const std::string& name): m_Name(name), m_Age(-1) {}explicit Entity(int age): m_Name("Unknown"), m_Age(age) {}
};void PrintEntity(const Entity& entity)
{// Printing
}int main()
{Entity a("Cherno");Entity b(22);Entity c = "Cherno";  Entity d = 22;PrintEntity(22);PrintEntity("Cherno");  PrintEntity(std::string("Cherno"));std::cin.get();
}

失效:

Entity d = 22;PrintEntity(22);

有效:

Entity e = Entity(22);Entity f(22);Entity g = (Entity)22;

什么时候使用explicit

使用数学库之类的东西,因为不想总是将数字变成向量,确保代码尽量安全

C++运算符及其重载

运算符

一个符号,通常代替一个函数来执行一些事情
dereference运算符->
+=, &, <<
new, delete
, () []

运算符重载

  • 给运算符重载赋予新的含义,添加参数或者创建
  • 允许在程序中定义或更改运算符的行为
  • 运算符重载的使用,应该是非常少

例3

#include <iostream>
#include <string>struct Vector2
{float x, y;Vector2(float x, float y): x(x), y(y) {}Vector2 Add(const Vector2& other) const{return Vector2(x + other.x, y + other.y);}Vector2 Multiply(const Vector2& other) const{return Vector2(x * other.x, y * other.y);}
};
int main()
{Vector2 position(4.0f, 4.0f);Vector2 speed(0.5f, 1.5f);Vector2 powerup(1.1f, 1.1f);// 加在一起Vector2 result = position.Add(speed.Multiply(powerup));std::cin.get();
}

Java只能这样写,因为Java没有操作符重载
可以用this指针:

Vector2 Add(const Vector2& other) const{return *this + other;}
Vector2 Add(const Vector2& other) const{return operator+(other);}

是否可以改成Vector2 result1 = position + speed * powerup;?

例4

#include <iostream>
#include <string>struct Vector2
{float x, y;Vector2(float x, float y): x(x), y(y) {}Vector2 Add(const Vector2& other) const{return Vector2(x + other.x, y + other.y);// return *this + other;}Vector2 operator+(const Vector2& other) const{return Add(other);}Vector2 Multiply(const Vector2& other) const{return Vector2(x * other.x, y * other.y);}Vector2 operator*(const Vector2& other) const{return Multiply(other);// return *this + other;}
};int main()
{Vector2 position(4.0f, 4.0f);Vector2 speed(0.5f, 1.5f);Vector2 powerup(1.1f, 1.1f);// 加在一起Vector2 result = position.Add(speed.Multiply(powerup));Vector2 result1 = position + speed * powerup;   // +运算符重载了std::cin.get();
}

运算符+和*都进行了重载

<< 操作符

std::cout << result2 << std::endl; // 

<< 操作符没有被重载,接收两个参数,一个是输出流cout,另一个是Vector2

例5

#include <iostream>
#include <string>struct Vector2
{float x, y;Vector2(float x, float y): x(x), y(y) {}Vector2 Add(const Vector2& other) const{return Vector2(x + other.x, y + other.y);// return *this + other;}Vector2 operator+(const Vector2& other) const{return Add(other);}Vector2 Multiply(const Vector2& other) const{return Vector2(x * other.x, y * other.y);}Vector2 operator*(const Vector2& other) const{return Multiply(other);// return *this + other;}std::ostream& operator<<(std::ostream& stream, const Vector2& other)
{stream << other.x << "," << other.y;return stream;
}
};
int main()
{Vector2 position(4.0f, 4.0f);Vector2 speed(0.5f, 1.5f);Vector2 powerup(1.1f, 1.1f);// 加在一起Vector2 result = position.Add(speed.Multiply(powerup));Vector2 result1 = position + speed * powerup;   // +, * 运算符重载了std::cout << result << std::endl;   // << 操作符重载了std::cin.get();
}

==操作符

例6

#include <iostream>
#include <string>struct Vector2
{float x, y;Vector2(float x, float y): x(x), y(y) {}Vector2 Add(const Vector2& other) const{return Vector2(x + other.x, y + other.y);// return *this + other;}Vector2 operator+(const Vector2& other) const{return Add(other);}Vector2 Multiply(const Vector2& other) const{return Vector2(x * other.x, y * other.y);}Vector2 operator*(const Vector2& other) const{return Multiply(other);// return *this + other;}std::ostream& operator<<(std::ostream& stream, const Vector2& other)
{stream << other.x << "," << other.y;return stream;
}
bool operator==(const Vector2& other) const{return x == other.x && y == other.y;}
};
int main()
{Vector2 position(4.0f, 4.0f);Vector2 speed(0.5f, 1.5f);Vector2 powerup(1.1f, 1.1f);// 加在一起Vector2 result1 = position.Add(speed.Multiply(powerup));Vector2 result12 = position.Add(speed.Multiply(powerup));Vector2 result1 = position + speed * powerup;   // +, * 运算符重载了std::cout << result2 << std::endl;   // << 操作符重载了
if (result1 == result2)      // == 操作符重载了{  }std::cin.get();
}

!=操作符

例7

#include <iostream>
#include <string>struct Vector2
{float x, y;Vector2(float x, float y): x(x), y(y) {}Vector2 Add(const Vector2& other) const{return Vector2(x + other.x, y + other.y);// return *this + other;}Vector2 operator+(const Vector2& other) const{return Add(other);}Vector2 Multiply(const Vector2& other) const{return Vector2(x * other.x, y * other.y);}Vector2 operator*(const Vector2& other) const{return Multiply(other);// return *this + other;}std::ostream& operator<<(std::ostream& stream, const Vector2& other)
{stream << other.x << "," << other.y;return stream;
}
bool operator==(const Vector2& other) const{return x == other.x && y == other.y;}bool operator!=(const Vector2& other) const{return !(*this == other);// return !operator==(other);}
};
int main()
{Vector2 position(4.0f, 4.0f);Vector2 speed(0.5f, 1.5f);Vector2 powerup(1.1f, 1.1f);// 加在一起Vector2 result1 = position.Add(speed.Multiply(powerup));Vector2 result12 = position.Add(speed.Multiply(powerup));Vector2 result1 = position + speed * powerup;   // +, * 运算符重载了std::cout << result2 << std::endl;   // << 操作符重载了
if (result1 == result2)      // == 操作符重载了{  }if (result1 != result2)      // != 操作符重载了{  }std::cin.get();
}

C++的this关键字

  • 通过它,可以访问成员函数,即属于某个类的函数
  • this是一个指向当前对象实例的指针,该方法属于这个对象实例
  • 可以写一个方法,非静态方法,为了调用这个方法,首先需要实例化一个对象,然后调用这个方法;这个方法必须用一个有效的对象来调用,关键字this是指向该对象的指针

例8

#include <iostream>
#include <string>class Entity
{
public:int x, y;Entity(int x, int y){this->x = x;this->y = y;}int GetX() const{const Entity* e = this;  // this必须是const的}
};
int main()
{std::cin.get();
}

this是const Entity const 类型
在这里插入图片描述

在类外调用函数,可以使用this

例9

#include <iostream>
#include <string>void PrintEntity(Entity* e);class Entity
{
public:int x, y;Entity(int x, int y){this->x = x;this->y = y;PrintEntity(this);}int GetX() const{const Entity* e = this;  // this必须是const的}
};void PrintEntity(Entity* e)
{// Printing
}int main()
{std::cin.get();
}
void PrintEntity(const Entity& e);
Entity& e = *this;PrintEntity(this);
void PrintEntity(const Entity& e)
{// Printing
}
int GetX() const{const Entity& e = *this;  // this必须是const的}
http://www.jmfq.cn/news/4896811.html

相关文章:

  • 做网站备完备案需要干什么/营销技巧和营销方法培训
  • 直播app开发价格/灰色行业关键词优化
  • 电子商务专业网站建设/购买网站域名
  • 微信公众号好看的模板哪里找/济南网站优化排名
  • 哪个网站可以免费做初级试题/拼多多关键词怎么优化
  • 棋牌网站开发搭建/电子商务推广
  • 温州做网站设计/网文推广怎么做
  • 天河做网站技术/手机关键词seo排名优化
  • 太原网站建设推广服务/seo优化常识
  • h5响应式网站建设/谷歌搜索引擎免费入口 台湾
  • html 网站模板/网站排名top排行榜
  • 如何申请一个网站 做视频/网络安全培训机构哪家好
  • 天津高端网站建设制作/网站外链的优化方法
  • 网络域名怎么注册/新野seo公司
  • 网站开发目的简介/游戏推广怎么做挣钱
  • 网站开发神书/网络培训平台有哪些
  • 公安局网站备案表/优化推广网站怎么做
  • 网站建设 全包 模板/百度推广的价格表
  • 建设银行登录网站/北京企业网站推广哪家公司好
  • 网络彩票的网站怎么做/建站abc网站
  • 杭州市建设信用网站/市场调研分析报告模板
  • 网站建设业务的途径/seo产品优化免费软件
  • 网站动画广告条怎么做的/重庆网
  • 青岛市城阳区建设局网站/营销策划方案案例范文
  • 苏州集团网站制作公司/最新新闻消息
  • 西安做网站服务/千锋教育怎么样
  • 西安网站建设公司都有哪些/热搜榜排名今日
  • 公司电脑为什么有的网站打不开/页面优化的方法
  • python的网站开发/俄罗斯搜索引擎
  • 做网站卖彩票/什么平台引流最快