
Exception C++摘记
文章平均质量分 74
yukuaishi
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
Iterators
#include using std::vector; int main() { vector e; copy(istream_iterator(cin), istream_iterator(), back_inserter(e)); // the find() algorithm returns its second argument(e.end()) if the value i原创 2011-12-02 12:35:31 · 198 阅读 · 0 评论 -
class mechanics
class Complex { public: // The constructor allows an implicit conversion. // Watch out for hidden temporaries created by implicit conversions. explicit Complex(double real, double imaginary = 0)原创 2011-12-12 10:09:35 · 267 阅读 · 0 评论 -
Auto_Ptr
auto_ptr source() { return auto_ptr( new T(1) ); } void sink( auto_ptr pt ) { } void f() { auto_ptr a( source() ); sink( source() ); sink( auto_ptr( new T(1) ) ); // It is never safe t原创 2011-12-21 15:50:14 · 235 阅读 · 0 评论 -
memory management 2
class B { public: virtual ~B(); void operator delete (void*, size_t) throw(); void operator delete[](void*, size_t) throw(); void f(void*, size_t) throw(); }; class D : public B { pub原创 2011-12-20 16:26:52 · 327 阅读 · 0 评论 -
Uses And Abuses of Inheritance
Use inheritance wisely. If you can express a class relationship using containment/delegation alone, you should always prefer that. If you need inheritance but aren't modeling IS-A, use nonpublic inher原创 2011-12-13 17:00:35 · 359 阅读 · 0 评论 -
exception the last one.
T t; T t(); T t(u); T t = u; // T t(T(u)); // When using return-by-value for non-builtin return types, prefer returning a const value. typedef int bool; const bool true = 1; const bool fals原创 2011-12-23 11:20:44 · 396 阅读 · 0 评论 -
Minimizing Compile-time Dependencies 1
// x.h: original header // #include // remove, unnecessary #include // std, typedef. it can replace with #include #include // for compatibility. #include // None of A, B, C, D or E are t原创 2011-12-14 13:49:21 · 204 阅读 · 0 评论 -
compilation firewalls
// In C++, when anything in a class definition changes (even private members), // all users of that class must be recompiled. To reduce these dependencies, // a common technique is to use an opaque原创 2011-12-14 14:21:28 · 317 阅读 · 0 评论 -
The "Fast Pimpl" Idiom
// file y.h class X; class Y { /*...*/ X* px_; }; // file y.cpp #include "x.h" Y::Y() : px_( new X ) {} Y::~Y() { delete px_; px_ = 0; } // This nicely hides X, but it turns out that Y is u原创 2011-12-14 16:46:44 · 293 阅读 · 0 评论 -
NameLookupAndInterfacePrinciple1
namespace A { struct X; struct Y; void f( int ); void g( X ); } namespace B { void f( int i ) { f( i ); // which f()? } void g( A::X x ) { g( x ); // which g()? }原创 2011-12-15 17:11:07 · 211 阅读 · 0 评论 -
Memory Management1
Prefer using the free store (new/delete). Avoid using the heap (malloc/free). const data stack free store Heap global/static原创 2011-12-19 14:25:54 · 255 阅读 · 0 评论 -
NameLookup4
// Example 2: Will this compile? // // In some library header: namespace N { class C {}; } int operator+(int i, N::C) { return i+1; } // solution this problem: let operator in N // A mainline to exer原创 2011-12-19 11:24:27 · 325 阅读 · 0 评论 -
Case-Insensitive
/* ci_string s( "AbCdE" ); // case insensitive // assert( s == "abcde" ); assert( s == "ABCDE" ); // still case-preserving, of course // assert( strcmp( s.c_str(), "AbCdE" ) == 0 ); assert( strcm原创 2011-12-02 15:39:05 · 973 阅读 · 0 评论 -
Maximally Reusable Generic Containers
template class fixed_vector { public: typedef T* iterator; typedef const T* const_iterator; iterator begin() { return v_; } iterator end() { return v_+size; } const_iterator begin() c原创 2011-12-02 16:03:00 · 326 阅读 · 0 评论 -
codecomplexity
// 1.invokes the Employee copy constructor. This copy operation might throw an exception String EvaluateSalaryAndReturnName(Employee e) { // 2.The Title() member function might itself throw, or it原创 2011-12-09 16:25:25 · 242 阅读 · 0 评论 -
writing exception cod2
template class Stack { public: Stack(); ~Stack(); Stack(const Stack& other); Stack& operator=(const Stack& other); size_t Count() const; void Push(const T&); // T* Pop(); void Pop(); T* T原创 2011-12-09 13:36:41 · 246 阅读 · 0 评论 -
Writing Exception Code
template class Stack { public: Stack(); ~Stack(); Stack(const Stack& other); Stack& operator=(const Stack& other); size_t Count() const; void Push(const T&); // T* Pop(); void Pop(); T* T原创 2011-12-07 16:40:52 · 291 阅读 · 0 评论 -
Temporary Objects
string FindAddr(list emps, string name) // const &, string& dangerous. { // for most containers, calling end() returns a temporary object that must be constructed and detroyed. for(list::iterator i原创 2011-12-06 12:54:58 · 227 阅读 · 0 评论 -
overring virtual functions
#include #include using namespace std; class Base { public: virtual void f( int ); virtual void f(double); virtual void g(int i = 10); }; void Base::f( int ) { cout << "Base::f(int)" <<原创 2011-12-12 13:39:58 · 422 阅读 · 0 评论 -
classrelationships1
class BasicProtocol /* : possible base classes */ // MessageCreator or MessageHelper. { public: BasicProtocol(); virtual ~BasicProtocol(); bool BasicMsgA( /*...*/ ); bool BasicMsgB( /*...*/原创 2011-12-12 14:28:22 · 297 阅读 · 0 评论 -
classrelationships2
class GenericTableAlgorithm { public: GenericTableAlgorithm(const string& table); virtual ~GenericTableAlgorithm(); // Process() returns true if and only if successful. // It does all the wor原创 2011-12-12 16:29:26 · 201 阅读 · 0 评论 -
NameLookupAndInterfacePrinciple2
//*** Example 5 (a) -- nonvirtual streaming class X { /*...ostream is never mentioned here...*/ }; ostream& operator<<( ostream& o, const X& x ) { /* code to output an X to a stream */ return o;原创 2011-12-15 17:12:11 · 196 阅读 · 0 评论