Timothy

链接

《More Effective C++》------ ( 4 )
《Exceptional C++》------ ( 2 )

《Exceptional C++》------ ( 1 )

Timothy posted @ 2010年7月08日 01:45 in Reading Notes , 1360 阅读

 条款1:迭代器

(1):无效的迭代器不可提取值, 例如 *end();

(2):对于成对待迭代器注意安全性,是否属于同一容器和是否先后顺序正确( STL的算法不检查这些安全 );

(3):注意迭代器失效的问题;

 

条款2~3:如何设计不区分大小写的string类

(1):注意string的定义, typedef basic_string<char, char_trait<char>, allocator<char> > string, 只需要继承一个 class my_char_trait : public char_trait<char>

    (字面翻译: 改写 字符特性( char trait ) )

 

条款4~5:具有最大通用性的复用容器

(1):模板构造函数和赋值函数,不会掩盖默认的隐藏的构造函数和赋值函数.

(2):要实现模板类的复制构造和赋值,需要重新实现特殊的成员模板函数,然后调用它( 与默认的形式是不一样的 ).

 

条款6:临时对象

(1):函数const reference传值,  函数单一入口单一出口,出口处采用显式构造函数返回对象, 使用++i而不是i++.

 

条款7:尽可能使用STL中的算法.

 

条款8~17:异常处理的安全性( difficult and fussy ).

 

条款18~19:代码的复杂性

(1):异常处理,会极大的增加代码的调用路径和复杂性。

 

 

 

条款20: class 设计技术

(1):避免单一参数的构造函数( 包括提供了N-1个默认参数的N参数构造 ),这样会造成隐式转换。

(2):a),函数参数传递采用by const reference not by object; b) 对于a = a + b, 如果支持应写成 a += b, 对于 a,b是非内置类型时,效率提升。

(3):对于运算符重载, 如果编写了 +, 应该同样编写 +=, 并且 + 通过 += 实现, 功能完整,且易于维护.

(4):操作符重载: 一元操作符应该是member function;

形如一元的, =, (), [], -> 应该是member, assignment版操作符( +=, -=, ... )应该是Member function

                其他的二元操作符应该是nonmember的.

(5):对于操作符重载,要注意函数的返回类型. 例如 +, 应该返回 const object, 避免 a + b = c出现, 例如 operator <<, 应该返回 stream&, 以便链式等式。

(6):对于类成员名称, 不要使用下划线开头, 例如 _real, 编译器的很多保留字采用这样的命名。

 

示例类:

 

//复数类

class CComplex 

{

public:

	explicit CComplex( double real, double imaginary =  0 )

		: m_dbReal( real ), m_dbImaginary( imaginary )

	{



	}



	CComplex& operator+= ( const CComplex& cref )

	{

		m_dbReal += cref.m_dbReal;

		m_dbImaginary += cref.m_dbImaginary;

		return *this;

	}



	CComplex& operator++()

	{

		++m_dbReal;

		return *this;

	}



	const CComplex operator++( int )

	{

		CComplex tmp( *this );

		++( *this );

		return tmp;

	}



	ostream& PrintSelf( ostream& os ) const

	{

		return os<<"("<<m_dbReal<<","<<m_dbImaginary<<")";

	}



private:

	double m_dbReal;

	double m_dbImaginary;

};



const CComplex operator +( const CComplex& lhs, const CComplex& rhs )

{

	CComplex tmp( lhs );

	return ( tmp += rhs );

}



ostream& operator<<( ostream& os, const CComplex& cref )

{

	return cref.PrintSelf( os );

}

 

条款21:改写虚函数

(1):在发生了隐藏时, 可以采用 using CBase::Func, 显式的声明父类函数。

(2):虚函数的调用时动态绑定,但是函数的默认参数值,是静态调用, 所以改写虚函数时,不要修改父类的默认参数值.

 

条款22:类之间的关系-1

(1):public inheritance 代表的是 Is-a 关系, 遵循Liskov替换法则。

LSP--Liskov Substitution Principle

定义:如果对于类型S的每一个对象o1,都有一个类型T的对象o2,使对于任意用类型T定义的程序P,将o2替换为o1,P的行为保持不变,则称S为T的一个子类型。

子类型必须能够替换它的基类型。LSP又称里氏替换原则。

(2):对于要塑造一个 A 是由 B 的实现而产生作用, 应该使用 成员/聚合, 或者私有继承实现。

    私有继承出现的场合:是需要Protected成员或者需要修改虚函数的实现。

条款23:类之间的关系-2

(1):关于一些设计模式思想的。

 

Avatar_small
Junior Certificate R 说:
2022年8月28日 17:59

Government of Bangladesh, Secondary and Higher Secondary Education Board has successfully completed those Junior School Certificate and Junior Dakil (Grade-8) annual final examination tests between 2nd to 11th November 2022 at all selected centers across the country along with Dhaka Division, Junior Certificate Result Dhaka Board and JSC & JDC Exams 2022 also completed successfully at all district schools of the Dhaka Board. According to the reports, there are a huge number of students are appeared for this Grade 8th standard terminal exams under Dhaka division and they are waiting for JSC Result 2022 Dhaka Board, right now the Secondary and Higher Secondary Education Board Dhaka has conducted the evaluation of answer sheet scripts to calculate subject wise and total marks of the student to announce JSC Result 2022 Dhaka Board.

Avatar_small
instagram löschen li 说:
2023年7月19日 22:03

Aus persönlichen Gründen werden einige Benutzer ihre Instagram-Konten löschen. Bevor Sie ihr Konto löschen, sollten Sie genau prüfen, ob Sie es wirklich wollen oder nicht. Weil es so einfach ist, sich anzumelden und Ihr Konto jederzeit zu kündigen. instagram löschen link Sobald Sie mit dem Löschen fortfahren, gibt es keine Möglichkeit, das Konto wiederherzustellen, und Sie verlieren alle Ihre Timeline-Posts, Reels, Follower, Kommentare und Favoriten.

Avatar_small
Tripura 3rd Class Q 说:
2023年8月29日 18:22

Tripura 3rd Class Model Question Paper 2024 are Prepared as per the updated Board of Education in the Tripura State Syllabus to help Students in their exam Preparations, which Aims to build both the Practical knowledge as well as analytical Skills for High School Every Student, SCERT Tripura Class Question Paper 2024 are Prepared as per the updated Tripura Board Class Syllabus to Tripura 3rd Class Question Paper 2024 help Students in their Exam Preparations.Tripura Class Question Paper 2024 will help a lot to the Students to Estimate their writing speed in the Examination 2024, After Completing the Preparation of entire Syllabus.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter