[Next] [Prev] [Up] [Top]

7.0 Classes

Access methods


7.8.1 Use inline for access methods where possible. (GL)

Both "get" and "set" access methods should be "inline" where the publication of a data "feature" fits the class abstraction. In otherwords, it is not reqiured that all data members have access methods.

7.8.2 Use const for "get" access methods. (R)

The "get" access methods should be const methods. Failure to do so will make it impossible to use a "get" method on a const object.

7.8.3 Avoid non- const "get" method return types (GL)

Never return a non-const reference to a data member in a "get" access method, unless it is a simple built-in type (ie, int or char). This used to cause the generation of a temporary since the "type" of "this" is "const", and is now an error (some compilers do not yet catch this).

Example:

class X

{

private:

TString fName;

public:

TString& name() const { return fName; } // bad

};

void foo()

{

TString& s = x.name (); // ref to temporary

...

};


No Title
[Next] [Prev] [Up] [Top]

Generated with CERN WebMaker

This site was grabbed using the TRIAL version of Grab-a-Site. This message does not appear on a licensed copy of Grab-a-Site.