In-depth understanding of C ++ mutable, using, decltype and other keywords

Depth understanding ++ C the mutable keywords
 
mutable in Chinese means "variable, variable", with constant (both C ++ in const) are antonyms.

  In C ++ in, mutable but also to break through the restrictions of const and settings. Mutable variables being modified, the variable will always be in a state, even in a const function.

  We know that if the member function does not change the state of the object, then this function is usually declared as a member of const. However, there are times when we need to modify some of the class status with independent data const member functions inside, then the data member should be mutalbe be modified.

  Here is a small example:

class ClxTest
{
 public:
  void Output() const;
};
 
void ClxTest::Output() const
{
 cout << "Output for test!" << endl;
}
 
void OutputTest(const ClxTest& lx)
{
 lx.Output();
}
 

  Output ClxTest class member function is used to output, the state does not modify the class is declared const so that the.

  OutputTest function is used to output, which calls the Output output method lx of the object, in order to prevent calling other member functions to modify any member variables in a function, so parameters can also be modified const.

  If now we want to add a function: calculates the output frequency for each object. If for the variable count is common variable, then the const member functions Output which can not modify the value of the variable; and the variable nothing to do with the state of the object, so it should be in order to modify the variable is removed const properties of the Output. This time, in relation to our mutable played - as long as mutalbe to modify the variable, all the problems solved.

  The following is a modified code:

class ClxTest
{
 public:
  ClxTest();
  ~ClxTest();
 
  void Output() const;
  int GetOutputTimes() const;
 
 private:
  mutable int m_iTimes;
};
 
ClxTest::ClxTest()
{
 m_iTimes = 0;
}
 
ClxTest::~ClxTest()
{}
 
void ClxTest::Output() const
{
 cout << "Output for test!" << endl;
 m_iTimes++;
}
 
int ClxTest::GetOutputTimes() const
{
 return m_iTimes;
}
 
void OutputTest(const ClxTest& lx)
{
 cout << lx.GetOutputTimes() << endl;
 lx.Output();
 cout << lx.GetOutputTimes() << endl;
}
 

  Counter m_iTimes is mutable modified, then it can break const limit, and can be modified in the const qualified function inside.

 

 

New meaning to the old C ++ 11 keyword (auto, a using, extern)


C ++ 11 keyword has been amended by adding a nullptr, constexpr, decltype, default, static_assert , etc., while the original keyword (auto, using, extern) meaning and purpose has been revised. Here a look at the main amendments to the auto, using, extern these three keywords.

auto
automated variable
auto = 12 is A;
auto B = 12.0f;
auto C = to true;
auto D = [] (int X) -> 12 is int {return;};
auto E = STD :: the bind (& FUNC, _1);


Binding delay
Template <typename T, typename L>
Auto Fun (T X, L Y) -> the decltype (X + Y) X {return;}


using
定义别名
template<typename T>
using Tlist = std::list<T>;
using Tlist = std::list<char>;
using df = void(*)();//等价于typedef void(*df)()


Use external configuration
using A :: A;


Introducing the exterior type
using typename A;


extern
outer shuttering
extern template <typename T> void ( T t);

 

 

Guess you like

Origin www.cnblogs.com/leijiangtao/p/12064618.html