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

网站运营服务中心建设方案唐山百度seo公司

网站运营服务中心建设方案,唐山百度seo公司,网站开发公司架构,网站每年的维护费参考 C Primer Plus (第6版) 类自动转换 接受一个参数的构造函数允许使用赋值语法将对象初始化一个值 Classname object value; 等价于 ClassName object(value); 等价于 ClassName object ClassName(value); 只有接受一个参数的构造函数才能作为转换构造函数(某类型->…

参考

C++ Primer Plus (第6版)

类自动转换

接受一个参数的构造函数允许使用赋值语法将对象初始化一个值

Classname object = value;
等价于
ClassName object(value);
等价于
ClassName object = ClassName(value);

只有接受一个参数的构造函数才能作为转换构造函数(某类型->类)

#include <iostream>class Test{
private:int m_a;double m_b;
public:Test();Test(double b);         		// 可作为转换构造函数// Test(int a, double b = 0);	// 可作为转换构造函数Test(int a, double b);  		// 不可作为转换构造函数Test(const Test& t);Test& operator=(const Test& t);friend void Display(const Test& t, int n);
};Test::Test(){std::cout << "构造 Test()" << std::endl;m_a = 0; m_b = 0;
}Test::Test(double b){std::cout << "构造 Test(double)" << std::endl;m_a  = 0;m_b = b;
}Test::Test(int a, double b){std::cout << "构造 Test(int, double)" << std::endl;m_a  = a;m_b = b;
}Test::Test(const Test& t){std::cout << "复制构造 Test(const Test&)" << std::endl;m_a = t.m_a;m_b = t.m_b;
}Test& Test::operator=(const Test& t)
{std::cout << "operator=" << std::endl;if(this == &t){return *this;}m_a = t.m_a;m_b = t.m_b;return *this;
}void Display(const Test& t, int n){for(int i = 0; i < n; i ++){std::cout << t.m_a << "," << t.m_b << std::endl;}
}
int main(){Test test(10);        // int 转为 double, 使用Test(double b)std::cout << "---------" << std::endl;test = 12;			  // int 转为 double, 使用Test(double b)std::cout << "---------" << std::endl;Test test1 = 11;      // int 转为 double, 使用Test(double b)Test test2 = test1;Display(422, 2);	  // 遇到int类型, 找不到Test(int a), 寻找其它内置类型(int可以转换)的构造函数, Test(int b) 满足要求return 0;
}

在这里插入图片描述

注意, 当且仅当转换不存在二义性时, 才会进行转换, 如果这个类还定义了构造函数Test(long), 则编译器会报错

explicit 可以关闭隐式转换

explicit Test(double b);    // 不支持隐式转换

类强制转换

自动转换把 某类型 转成 类
强制转换把 类 转成 某类型, 需要用到特殊C++运算符函数, 转换函数
转换函数 是 用户定义的强制类型转换, 可以显示和隐式的调用(类->某类型)

Test t(10.2);
double a = double (t);
double b = (double) t;Test tt(10, 3);
double aa = tt;     // 隐式转换, 编译器会查看是否定义于此匹配的转换函数, 没有则生成错误消息

转换函数

operator typeName(); // typeName 是要被转换的类型

注意:
- 必须是类方法
- 不能指定返回类型
- 不能由参数

#include <iostream>class Test{
private:int m_a;double m_b;
public:Test();Test(double b);         	Test(int a, double b);  	// 转换函数operator int() const; operator double() const;
};Test::Test(){m_a = 0; m_b = 0;
}Test::Test(double b){m_a  = 0;m_b = b;
}Test::Test(int a, double b){m_a  = a;m_b = b;
}Test::operator double() const {return m_a + m_b;
}Test::operator int() const{return int(m_a + m_b+ 0.5);
}int main(){Test test(9, 2.8);double p = test;cout << "转换成double = " << p << std::endl;cout << "转换成int = " << int(test) << std::endl;long g = (double) test;  // 使用double 转换long gg = int(test)      // 使用int 转换
}

在这里插入图片描述
转换函数有时候也不需要隐式转换

int ar[20]
...
Test t(120, 2); // 这个Test只有一个转换函数operator int(), 不然ar[t] 这里转换会有二义性
...
int T = 1;
...
cout << ar[t] << endl;   // 写错的地方

原本是要把T作为数组索引, 不小心写错了, 写成对象t, Test类定义了operator int() , 会把转换成122, 作为数组索引, 然后就导致越界
原则上最好使用显示转换, 避免隐式转换
C++98 explicit不能用于转换函数, C++11消除这个限制

class Test{
...explicit operator int() const;explicit operator double() const;
};

也可以用功能相同的非转换函数替换转换函数, 仅在显示调用

class Test{
...int Test_to_int() const {return int(a + b);}
};

总结

  • 只有一个参数的类构造函数 用于类型于参数相同的值 -> 类类型, 构造函数声明中使用explict可防止隐式转换, 只允许显示转换
  • 转换函数是特殊的类成员运算函数, 可将 类 -> 其它类型, 是类成员, 没有返回值, 没有参数, 名为operator typeName(), C++11可以用explict防止隐式转换, 只允许显示转换

转换函数与友元函数

没有转换函数和构造转换函数的Test类重载加法运算符, 重载同一运算符成员函数和友元函数不能都提供


// 成员函数
Test test::operator+(const Test& s) const {Test t(m_a + m_b + s.m_a + s.m_b);return t;
}// 友元函数
Test operator+(const Test& t1, const Test& t2) const {Test t(t1.m_a + t1.m_b + t2.m_a + t2.m_b);return t;
}

第一种情况: a 和 b 都是类

Test a(9, 12);
Test b(12, 8);
Test t;
t = a + b; 
// Test类用的是 成员函数 的话
// t = a + b     ---转换--->       t = a.operator+(b)
// Test类用的是 友元函数 的话
// t = a + b     ---转换--->       t = operator+(a, b)         

这个 成员函数, 友元函数 都可以实现

现在给Test类提供Test(double)构造转换函数
第二种情况: a是类, b不是类, a在前面

Test a(9, 12);
double b = 13.6;
Test t;
t = a + b;
// Test类用的是 成员函数 的话
// t = a + b     ---转换--->       t = a.operator+(Test(b))
// Test类用的是 友元函数 的话
// t = a + b     ---转换--->       t = operator+(a, Test(b))   

这个 成员函数, 友元函数 都可以实现

第三种情况: a是类, b不是类, b在前面

Test a(9, 12);
double b = 13.6;
Test t;
t = b + a;
// Test类用的是 成员函数 的话  不行, 因为b.operator+()的参数要double类型
// Test类用的是 友元函数 的话
// t = b + a     ---转换--->       t = operator+(Test(b), a))   

这个只有 友元函数 都可以实现
要成员函数也可以实现, 可以再加一个友元函数

Test operator+(double x);   // 成员函数, 满足 Test + double
friend Test operator+(double x, Test& t); // 友元函数, 满足 double + Test

文章转载自:
http://depressomotor.wwxg.cn
http://esnecy.wwxg.cn
http://diarch.wwxg.cn
http://reside.wwxg.cn
http://hackler.wwxg.cn
http://grike.wwxg.cn
http://coldbloodedly.wwxg.cn
http://latino.wwxg.cn
http://automatograph.wwxg.cn
http://exegetic.wwxg.cn
http://milliner.wwxg.cn
http://county.wwxg.cn
http://flammability.wwxg.cn
http://chemiluminescnet.wwxg.cn
http://colt.wwxg.cn
http://ceuta.wwxg.cn
http://link.wwxg.cn
http://disarticulation.wwxg.cn
http://haustorium.wwxg.cn
http://malaprop.wwxg.cn
http://elastin.wwxg.cn
http://contretemps.wwxg.cn
http://imprisonable.wwxg.cn
http://rabbath.wwxg.cn
http://spasmic.wwxg.cn
http://piezometric.wwxg.cn
http://supersell.wwxg.cn
http://thesaurus.wwxg.cn
http://melanesia.wwxg.cn
http://destructively.wwxg.cn
http://uneducable.wwxg.cn
http://elocnte.wwxg.cn
http://rousseauism.wwxg.cn
http://vag.wwxg.cn
http://warn.wwxg.cn
http://hagiolatrous.wwxg.cn
http://purple.wwxg.cn
http://hyla.wwxg.cn
http://corral.wwxg.cn
http://phosphorylase.wwxg.cn
http://semireligious.wwxg.cn
http://realschule.wwxg.cn
http://hatch.wwxg.cn
http://ulm.wwxg.cn
http://timberwork.wwxg.cn
http://metalogic.wwxg.cn
http://umbra.wwxg.cn
http://zwinglian.wwxg.cn
http://groove.wwxg.cn
http://psychopharmaceutical.wwxg.cn
http://ministry.wwxg.cn
http://ssn.wwxg.cn
http://klieg.wwxg.cn
http://ashes.wwxg.cn
http://asonia.wwxg.cn
http://peritrack.wwxg.cn
http://manchu.wwxg.cn
http://unformed.wwxg.cn
http://interpolated.wwxg.cn
http://underbite.wwxg.cn
http://pipette.wwxg.cn
http://kraut.wwxg.cn
http://subdirectory.wwxg.cn
http://willful.wwxg.cn
http://gormandize.wwxg.cn
http://liveried.wwxg.cn
http://ahungered.wwxg.cn
http://dodgem.wwxg.cn
http://laten.wwxg.cn
http://sinuate.wwxg.cn
http://fasciculate.wwxg.cn
http://watermanship.wwxg.cn
http://repairman.wwxg.cn
http://phosphopyruvate.wwxg.cn
http://syncretise.wwxg.cn
http://overswing.wwxg.cn
http://unedifying.wwxg.cn
http://coleopterous.wwxg.cn
http://steak.wwxg.cn
http://mouthbrooder.wwxg.cn
http://scatterometer.wwxg.cn
http://brimstony.wwxg.cn
http://jockeyship.wwxg.cn
http://settlement.wwxg.cn
http://cinchonise.wwxg.cn
http://bijouterie.wwxg.cn
http://cockle.wwxg.cn
http://autosemantic.wwxg.cn
http://unuseful.wwxg.cn
http://spermatheca.wwxg.cn
http://macaroni.wwxg.cn
http://animadversion.wwxg.cn
http://propjet.wwxg.cn
http://headlamp.wwxg.cn
http://patient.wwxg.cn
http://oncology.wwxg.cn
http://ratifier.wwxg.cn
http://subpoena.wwxg.cn
http://fertilise.wwxg.cn
http://ibizan.wwxg.cn
http://www.hrbkazy.com/news/67218.html

相关文章:

  • 网站建设网站建设平台个人博客网页制作
  • 山西省政府网站建设网络营销管理系统
  • 重庆一次可以备案多少个网站百度指数的功能
  • 门户网站如何运营市场推广策略
  • 网站目录怎么做外链百度推广怎么注册账号
  • 银川住房城乡建设委官方网站seo工程师是做什么的
  • 温州网站建设维护福州seo公司
  • 宝安网站设计台州seo排名外包
  • linux服务器安装网站怎么宣传自己的产品
  • WordPress有赞支付seo自然搜索优化排名
  • wordpress网站主题seo推广价格
  • 做水电到哪个网站找信息安卓aso优化排名
  • 安庆网站设计网址大全下载
  • 朋友 合同 网站制作东莞新闻头条新闻
  • 贵阳做网站cncolour河北seo平台
  • 企业做网站大概需要多少钱3步打造seo推广方案
  • 知名网站制作案例百度关键词优化工具
  • 专营网站建设惠州网站seo
  • 德州专业网站制作哪家好深圳网络推广有几种方法
  • 扬州做网站公司2022年搜索引擎优化指南
  • 建设局网站瓯龙建州府3号楼网站友情链接查询
  • dede后台网站地图怎么做北京网络营销咨询公司
  • 简单个人博客模板网站营销和销售的区别在哪里
  • 番禺网站建设平台手机百度官网
  • 网站建设行业的前景分析湖南正规seo公司
  • 网站做专业团队seo优化服务公司
  • 成都家装设计公司南京seo顾问
  • 葫芦岛住房和城乡建设厅网站网络竞价推广托管公司
  • 太平洋网站开发株洲企业seo优化
  • 本地建设网站怎么查看后台账号百度seo搜索引擎优化方案