现在的位置: 首页 > 综合 > 正文

[NOTES] Effective C++

2014年02月03日 ⁄ 综合 ⁄ 共 1961字 ⁄ 字号 评论关闭

[Item 19]:
the copy constructor defines how pass-by-value is implemented for a type.
If you wish to allow other classes to inherit from your class, that affects whether the functions you declare are virtual, especially your destructor.

on the off  chance:  In the slight hope or possibility.

[Item 20]:
By default, C++ passes objects to and from functions by value
pass by reference-to-constif you have an object of a built-in type (e.g., an int), it's often more efficient to pass it by value than by reference

bool validateStudent(const Student& s);

Passing parameters by reference also avoids the slicing problem

[Item 21]:
a reference is just a name, a name for some existing object.
A function can create a new object in only two ways: on the stack or on the heap.
Heap-based objects come into being through the use of new
Like all designs employing the use of static objects, this one immediately raises our thread-safety hackles

Never return a pointer or reference to a local stack object, a reference to a heap-allocated object, or a pointer or reference to a local static object if there is a chance that more than one such object will be needed

[ITEM 22]
Rarely does every data member need a getter and setter.
Public means unencapsulated, and practically speaking, unencapsulated means unchangeable,
Aren't protected data members more encapsulated than public ones? Practically speaking, the surprising answer is that they are not.
The encapsulatedness of a data member, then, is inversely proportional to the amount of code that might be broken if that data member changes,

[ITEM 23]

our changes directly affect only those things that can see what we change
only members and friends have access to private members.
the choice yielding greater encapsulation is the non-member non-friend function, because it doesn't increase the number of functions that can access the private parts of the class.
In C++, a more natural approach would be to make clearBrowser a non-member function in the same namespace as WebBrowser:convenience functions
Putting all convenience functions in multiple header files — but one namespace — also means that clients can easily extend the set of convenience functions.

namespace WebBrowserStuff {
    
class WebBrowser { ... };
       
void clearBrowser(WebBrowser& wb);
  ...
}

 

抱歉!评论已关闭.