... | @@ -242,7 +242,7 @@ public: |
... | @@ -242,7 +242,7 @@ public: |
|
// This rule was deprecated.
|
|
// This rule was deprecated.
|
|
//bool equals(const Foo& other) const;
|
|
//bool equals(const Foo& other) const;
|
|
bool operator==(const Foo& other) const { return (equals(other)); }
|
|
bool operator==(const Foo& other) const { return (equals(other)); }
|
|
}
|
|
};
|
|
```
|
|
```
|
|
|
|
|
|
## Explicit Constructors
|
|
## Explicit Constructors
|
... | @@ -253,7 +253,7 @@ class Foo { |
... | @@ -253,7 +253,7 @@ class Foo { |
|
public:
|
|
public:
|
|
// Constructor with one argument
|
|
// Constructor with one argument
|
|
explicit Foo(std::string name);
|
|
explicit Foo(std::string name);
|
|
}
|
|
};
|
|
```
|
|
```
|
|
|
|
|
|
## Class Attributes
|
|
## Class Attributes
|
... | @@ -410,28 +410,42 @@ methodName(int argument_one, std::string message, |
... | @@ -410,28 +410,42 @@ methodName(int argument_one, std::string message, |
|
|
|
|
|
### Virtual Methods
|
|
### Virtual Methods
|
|
|
|
|
|
Explicitly add `virtual` to method declarations in derived classes:
|
|
Explicitly add `override` to method declarations in derived classes:
|
|
|
|
|
|
```cpp
|
|
```cpp
|
|
|
|
|
|
class Base {
|
|
class Base {
|
|
// this 'virtual' is absolutely necessary
|
|
// this 'virtual' is absolutely necessary
|
|
virtual void toBeVirtual();
|
|
virtual void foo();
|
|
};
|
|
};
|
|
|
|
|
|
class Derived : public Base {
|
|
class Derived : public Base {
|
|
// this 'virtual' is not necessarily needed, but add it per this guideline
|
|
// 'override' is recommended per this guideline
|
|
virtual void toBeVirtual();
|
|
// 'virtual' is not necessarily needed here, but it may be added if the
|
|
}
|
|
// class is further derived.
|
|
|
|
void foo() override;
|
|
|
|
};
|
|
```
|
|
```
|
|
|
|
|
|
This way it's easier to recognize `Derived::toBeVirtual()` is (more
|
|
This way, the compiler will display an error message if the method in the base
|
|
likely to be) defined as a virtual method in the `Base` class without
|
|
class is not virtual or a method with the same signature doesn't exist:
|
|
referring to the base class definition. It could also mean that
|
|
|
|
`toBeVirtual` is not defined in the base class and is intended to work
|
|
```
|
|
as a virtual methods for classes derived further from `Derived`, but
|
|
error: ‘void Derived::foo()’ marked ‘override’, but does not override
|
|
in practice that's a very rare case; in most cases we use these classes
|
|
```
|
|
through the (top) base class interfaces.
|
|
|
|
|
|
Also, once `override` is used, if another method in the same translation unit
|
|
|
|
overrides a virtual function, but is not marked accordingly, a compiler warning
|
|
|
|
will be raised:
|
|
|
|
|
|
|
|
```
|
|
|
|
warning: 'foo' overrides a member function but is not marked 'override' [-Winconsistent-missing-override]
|
|
|
|
```
|
|
|
|
|
|
|
|
`override` is a C++11 construct. Before C++11 was adopted, `virtual` was used to
|
|
|
|
signal overriding of methods. There are a few disadvantages with this rule. The
|
|
|
|
compiler doesn't help in enforcing it. There's a false sense of safety around
|
|
|
|
it. And it prevents the developer from declaring the method in the derived class
|
|
|
|
as non-virtual if the design requires it.
|
|
|
|
|
|
### Const references
|
|
### Const references
|
|
|
|
|
... | @@ -442,7 +456,7 @@ This includes smart pointers, some of them can be relatively expensive to copy. |
... | @@ -442,7 +456,7 @@ This includes smart pointers, some of them can be relatively expensive to copy. |
|
```cpp
|
|
```cpp
|
|
void
|
|
void
|
|
function(const boost::shared_ptr<DataType>& param) {
|
|
function(const boost::shared_ptr<DataType>& param) {
|
|
...
|
|
...
|
|
}
|
|
}
|
|
```
|
|
```
|
|
|
|
|
... | @@ -465,7 +479,7 @@ If a function has a parameter with a "top-level const", make sure the `const` ap |
... | @@ -465,7 +479,7 @@ If a function has a parameter with a "top-level const", make sure the `const` ap |
|
```cpp
|
|
```cpp
|
|
int
|
|
int
|
|
foo(const int param) {
|
|
foo(const int param) {
|
|
...
|
|
...
|
|
}
|
|
}
|
|
```
|
|
```
|
|
|
|
|
... | | ... | |