Item Effective C++ 条款 item_20
Avoid data members in the public interfaceThis rule warns you if data members are present in the public interface. Instead of making data members public, you should use functions.
Reason for rule: Avoiding data members in the public interface provides more precise control over accessibility of data members, reduces confusion for callers, and enables functional abstraction (allowing the replacement of data members with computations without impacting users of the class). It also improves consistency, flexibility, and access-control.
Example
/*
* Item 20 - Avoid data members in the public interface
*/
class A
{
public:
int _idata2; // Effective C++ 条款 item item 20 violation
int ReadData1(void) { return _idata1; }
void WriteData1(int ival) { _idata1 = ival; }
private:
int _idata1;
};
int main()
{
return 0;
}
Output
Avoid data members in the public
interface
Violation: Effective C++ item 20
Public data members for class A:
_idata2
Item Effective C++ 条款 item_22
Pass and return objects by reference instead of by valueThis rule detects where an object has been passed by value instead of by reference.
Reason for rule: Passing and returning objects by reference is more efficient than passing by value because no new objects are being created and because it avoids the "slicing problem."
Exception: There are situations in which you cannot pass an object by reference (see Item Effective C++ 条款 item_23). There also may be a situation where the object is so small that it would be more efficient to pass by value instead of by reference.
Example
/*
* Item 22 - Pass and return objects by reference instead
* of by value
*/
class A
{
public:
A() {}
A foo(const A a) // 2 Effective C++ 条款 item item 22 violations
{ // passing variables into foo by value
return a; // returning by value when it is not required
}
};
int main()
{
return 0;
}
Output
Pass objects by reference instead of
by value
Violation: Effective C++ item 22
Parameter a of function foo passed by value
Return objects by reference instead of by
value
Violation: Effective C++ item 22
Function foo returns object by value
Item Effective C++ 条款 item_23
Don't try to return a reference when you must return an object
This rule detects when you return a reference where you are supposed to return an object.
Reason for rule: Returning a reference instead of an object will result in corrupt data or a memory leak.
Example
/*
* Item 23 - Don't try to return a reference when you must
* return an object.
*/
class A
{
public:
A(int num = 0, int den = 1) : _num(num), _den(den) {}
int numerator() const { return _num; }
int denominator() const { return _den; }
private:
int _num;
int _den;
};
// Effective C++ 条款 item item 23 violation
A& operator*(const A& lhs, const A& rhs)
{
A temp(lhs.numerator() * rhs.numerator(),
lhs.denominator() * rhs.denominator());
return temp;
}
int main()
{
return 0;
}
Output
Don't try to return a reference when you
must return an object
Possible severe violation: Effective C++ item 23
Operator * should return an object
Item Effective C++ 条款 item_25
Avoid overloading on a pointer and a numerical typeThis rule detects when you are overloading on a pointer and a numerical type.
Reason for rule: Calling with an argument of zero will invoke the numerical type even though it is intuitively ambiguous.
Example
/*
* Item 25 - Avoid overloading on a pointer and a numerica
* type.
*/
class A
{
public:
int func(char *ch)
{
char c=*ch;
return 0;
}
int func(int i) // Effective C++ 条款 item item 25 violation
{
int var=i;
return 0;
}
};
int main()
{
return 0;
}
Output
Avoid overloading on a pointer and a
numerical type
Violation: Effective C++ item 25