Protection of C + + shared data (Chapter 5)

Posted by isam4m on Wed, 12 Jan 2022 23:06:07 +0100

catalogue

Protection of shared data

Constant type

l regular members

l constant array: array elements cannot be updated (see Chapter 6 for details).

Constant object

Example 5-7 # constant member function example

Example 5-8 example of constant data member

Example 5-9} often cited as formal parameters

Protection of shared data

l) data that needs to be shared and prevented from change should be declared as a constant type (decorated with const).

l) member functions that do not change the object state should be declared as constant functions.

Constant type

l constant object: it must be initialized and cannot be updated.

n # const class name object name

l regular members

n) class members decorated with const: constant data members and constant function members

l constant reference: the referenced object cannot be updated.

n # const # type specifier & reference name

l constant array: array elements cannot be updated (see Chapter 6 for details).

n , type specifier , const , array name [size]

l constant pointer: pointer to constant (see Chapter 6 for details).

Constant object

l object decorated with const

l example:

class A

{

  public:

    A(int i,int j) {x=i; y=j;}

                     ...

  private:

    int x,y;

};

A const a(3,4); //A is a constant object and cannot be updated

l think: which operations are in danger of trying to change the state of the constant object?

Regular member

l object members decorated with const

l constant member function

n) functions described with const keyword.

n # constant member functions do not update the data members of the object.

n # constant member function description format:

Type specifier: function name (parameter table) const;

Here, const is a component of the function type, so the const keyword should also be carried in the implementation part.

The n , const keyword can be used to distinguish overloaded functions

l only its constant member functions can be called through a constant object.

l constant data member

n , use const to describe the data member.

Example 5-7 # constant member function example

#include<iostream>

using namespace std;

class R {

public:

  R(int r1, int r2) : r1(r1), r2(r2) { }

  void print();

  void print() const;

private:

  int r1, r2;

};

 

void R::print() {

  cout << r1 << ":" << r2 << endl;

}

void R::print() const {

  cout << r1 << ";" << r2 << endl;

}

int main() {

  R a(5,4);

  a.print(); //Call void print()

  const R b(20,52); 

  b.print(); //Call void print() const

       return 0;

}

 

Example 5-8 example of constant data member

#include <iostream>

using namespace std;

class A {

public:

       A(int i);

       void print();

private:

       const int a;

       static const int b;  //Static constant data member

};

 

const int A::b=10;

A::A(int i) : a(i) { }

void A::print() {

  cout << a << ":" << b <<endl;

}

int main() {

//Create objects a and b, and call constructors with 100 and 0 as initial values,

//Assign initial values to the constant data members of the object through the initialization list of the constructor

  A a1(100), a2(0);

  a1.print();

  a2.print();

  return 0;

}

Often cited

l if const is used when declaring a reference, the declared reference is a constant reference.

l frequently referenced objects cannot be updated.

l if you use constant references as formal parameters, you will not accidentally change the arguments. The frequently cited declaration forms are as follows:

n # const # type specifier & reference name;

 

Example 5-9} often cited as formal parameters

#include <iostream>

#include <cmath>

using namespace std;

class Point { //Point class definition

public:          //External interface

       Point(int x = 0, int y = 0)

    : x(x), y(y) { }

       int getX() { return x; }

       int getY() { return y; }

       friend float dist(const Point &p1,const Point &p2);

private:         //Private data member

       int x, y;

};

 

float dist(const Point &p1, const Point &p2) {

       double x = p1.x - p2.x; 

       double y = p1.y - p2.y;

       return static_cast<float>(sqrt(x*x+y*y));

}

 

int main() {  //Main function

       const Point myp1(1, 1), myp2(4, 5);    

       cout << "The distance is: ";

       cout << dist(myp1, myp2) << endl;

       return 0;

}

Topics: C++ p2p