Main Content

Method not const

A method that can be made const is not marked const

Since R2022a

Description

This defect occurs when a method that meets these criteria is not marked as a const:

  • The method is not virtual.

  • The method does not return pointers or references to nonconst data.

  • The method does not modify any variables, does not call global functions, and does not call any nonconst member functions.

Risk

If a method in your code satisfies the requirements of being a const method, then declaring the method as a const has these advantages:

  • Objects that are const are not allowed to invoke nonconst methods. When you declare a method as const, some additional references and objects in your code might be declared as const, increasing the const correctness of your code.

  • The C++11 standard assumes that const methods are free of data races. Using such methods in multiple threads does not require external synchronization, for instance, by using mutex objects. Using const method makes concurrent programming convenient.

Not declaring eligible methods a const makes maintaining the const correctness of your code more difficult. Code that is not const-correct remains vulnerable to unexpected mutation of variables, unexpected assignments, and inefficient compiler optimization.

Fix

To fix this defect, declare eligible methods as const. If a const overload of the function already exists, consider removing the nonconst overload.

Examples

expand all

 class LossyPolymer{
	double n;
	double k;
	public:
	LossyPolymer(double n_in, double k_in):n(n_in), k(k_in){}
	double getN() {
		return n;
	}
	double getk() {
		return k;
	}
	
};

void foo(){
	LossyPolymer PMMA = LossyPolymer(1.5,0.001);
	const LossyPolymer PVK = LossyPolymer(1.6,0.01);
	int k_PMMA = PMMA.getk();
	//int k_PVK = PVK.getk(); Compile error
}

In this example, a class LossyPolymer is implemented that represents the optical properties of polymer materials. The optical properties of a LossyPolymer is represented by the private variables n and k. The class has getter methods for these variables that return a copy of the private variables. The getter methods of LossyPolymer do not modify any variable, but they are not declared const, perhaps inadvertently.

The class LossyPolymer represents a physical material. A physical material has constant optical properties. Objects of the class LossyPolymer are expected to be const objects, such as PVK. Because getk() is nonconst, the const object PVK cannot call its getK() method. To use the getter, you must declare instances of LossyPolymer as nonconst objects, which is not the best practice. The incorrect constness of the getter function prevents const-correct behavior in the code. Polyspace® flags them.

Correction — Declare Eligible Methods as const

To fix this defect, declare the getter methods as const. Doing so allows you to declare PVK and PMMA as const objects and call the getter functions.

 class LossyPolymer{
	double n;
	double k;
	public:
	LossyPolymer(double n_in, double k_in):n(n_in), k(k_in){}
	const double getN() const{
		return n;
	}
	const double getk() const{
		return k;
	}
	
};

void foo(){
	LossyPolymer PMMA = LossyPolymer(1.5,0.001);
	const LossyPolymer PVK = LossyPolymer(1.6,0.01);
	int k_PMMA = PMMA.getk();
	int k_PVK = PVK.getk();
}

Result Information

Group: Good practice
Language: C++
Default: Off
Command-Line Syntax: METHOD_NOT_CONST
Impact: Low

Version History

Introduced in R2022a

expand all