- 浏览: 401273 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (309)
- xaml C# wpf (0)
- scala java inner clas (1)
- Tools UML Eclipse UML2 (1)
- Timer .NET Framework (1)
- perl (6)
- python function paramter (1)
- Python Docstring (1)
- Python how to compare types (1)
- Python (8)
- java (5)
- C# (76)
- C# WPF (0)
- p4 (0)
- WPF (46)
- .net (6)
- xaml (1)
- javascript (40)
- windows (10)
- scala (4)
- winform (1)
- c++ (48)
- tools (12)
- cmd (1)
- os (0)
- CI (0)
- shell (0)
- C (2)
- haskell (49)
- functional (1)
- tool (1)
- gnu (1)
- linux (1)
- kaskell (0)
- svn (0)
- wcf (3)
- android (1)
最新评论
what are the special functions, the special function includes the following.
- constructor - initialization
- destructor - destruction
- assignment
- memmory management
- type conversion
what we are going to do discuss here is the const and volatile member function.
Const member:
In general , any class that is spected to be used extensivly should declare the member functions that do not modify the class data members as const members.
By const member does not change the class members. That is only half the truth, the user has to be vigilant because a constant member does not guarantee that everything to which the classs object referes will remain invariant throughtout he invocation of the meber function.
First let' see the following class definition.
/** * In general , any class that is spected to be used extensivly should declare the member functions that do not modify the class data members as const members. */ class Text { public: void bad(const string ¶m) const; private: char * _text; };
and then let's see the code as below., which shows that the C++ compiler does not detect the folowing violation because of pointer semantic.
/** * though you cannot directly assign some value to class members, but the compiler is not able to guarantee that everything to which the class object referes will remain invariant throughout the * invocation of hte member function */ void Text::bad(const string ¶m) const { _text = param.c_str(); // error: _text cannot be modified for (int ix = 0; ix < param.size(); ++ix) { _text[ix] = param[ix]; // bad style but not an error } }
Let's see how you normally use the const mebers...
class Screen { public: bool isEqual(char ch) const; /** overload of the const and non-const mmber functions * */ char get(int x, int y); char get(int x, int y) const; };
and only the const class object can invoke the const members... here is the code.
void test_const_members() { const Screen cs; Screen s; char ch = cs.get(0, 0); // calls the const membres ch = s.get(0, 0); // calls the non-const members }
volatile members
it is less common that you will use this, but a member or a constructor a destructor can be declared as teh volatile member function.
as with the const members, only volatile class object can access the volatile members.
let's see an example.
class Screen { public: /* * const member is easy to understand , but what about the volatile members * * A class object is declared volatile if its value can possibly be changed in ways outside eithe rthe control or detection of the compilers (for example), * only volatile members functions, constructors, and the destructor can be inovked by a volatile class object. */ void poll() volatile; };
below is the code that test you how to use the volatile member function.
void test_volatile_member() { volatile Screen vs; vs.poll(); vs.isEqual('a'); // error: you can only call volatile member function by a volatile clas object }
发表评论
-
不安装Visual Studio,只用Windows SDK搭建VC环境
2013-12-31 21:52 15345首先你需要下载的是 Microsoft Windows S ... -
rpath - runtime search path
2013-04-03 11:36 1021RPath is a very interesting to ... -
C++ - autogenerated copy constructor and assignment operator gotchas
2013-01-24 13:32 774It has been changed that the s ... -
c++ - rethrow a exception gotchas
2012-12-23 10:57 972As in my prevoius example in j ... -
c++ -typeid operator
2012-10-15 22:30 1065typeid is the one of the meager ... -
c++ - dynamic_cast revisit
2012-10-14 21:21 781There are several built-in type ... -
c++ - virtual inheritance example 1
2012-10-14 15:25 830we have discussed the virtual i ... -
c++ - virtual inheritance
2012-10-12 08:58 993As we have discussed in the pos ... -
c++ type of inheritance
2012-09-28 08:58 756There are 3 types of inheritanc ... -
c++ - vritually virtual new
2012-09-27 23:59 963Let's see what if we want to cl ... -
c++ - virtual destructor
2012-09-27 22:01 981As we all know that virtual des ... -
c++ - vritual function and default arguments
2012-09-27 08:56 1001As we all know that we virtual ... -
c++ - template specialization and partial specialization
2012-09-26 22:38 1337in this post, we are going to e ... -
c++ - member template in class template
2012-09-26 08:19 944class member template can be us ... -
c++ template class and the pattern to use its friends
2012-09-25 23:47 992template class may defined thei ... -
c++ - Friend declaration in class Template
2012-09-25 08:47 1215There are three kinds of friend ... -
c++ - class template default parameters
2012-09-25 08:18 861the template has parameter, it ... -
c++ - operator new and delete and an example of linked list stores by new/delete
2012-09-24 07:53 594The operator new and delete ope ... -
c++ - delete(void *, size_t) or delete(void *)
2012-09-24 07:18 1174In my previous dicuss, we have ... -
c++ - placement operator new() and the operator delete()
2012-09-23 15:22 877A class member operator new() c ...
相关推荐
在C++编程语言中,`const`和`volatile`是两个非常重要的关键字,它们用于修饰变量,赋予变量特殊的属性。这两个关键字在理解程序的行为、内存模型以及多线程编程中起到至关重要的作用。在此,我们将深入探讨`const`...
c++ 的array源码分析和reverse-iterator和-Array-const-iterator类
详细介绍Const与Volatile的相同与不同
EFFECTIVE C++ 条款03 尽量使用const 思维导图 在 C++ 编程中,使用 const 关键字可以提高代码的可读性、可维护性和安全性。本文将详细介绍 EFFECTIVE C++ 的第三条款:尽量使用 const 思维导图。 一、const ...
initializer for array of non-const data member(解决方案).md
在C++和C编程语言中,`const`关键字是一个非常重要的概念,它涉及到常量、常量指针、指针常量以及常量指针常量等多方面的知识。`const`关键字的主要作用是限制变量的修改,以提高程序的安全性和可读性。下面将详细...
在C++编程中,`const`和`volatile`是两个非常重要的关键字,它们用于修饰变量的属性,帮助我们更好地控制和理解代码的行为。本文将详细探讨这两个关键字的含义、作用以及如何正确使用它们。 一、关键字`const` 1. ...
### static、const、volatile用法解析 在编程领域中,`static`、`const`、`volatile` 这三个关键字非常常见且重要。它们分别用于控制变量的作用域、可变性和不可预测性,是理解程序行为的基础之一。下面将详细介绍...
### const extern static volatile 小结 #### 一、Const(常量) `const` 关键字在 C/C++ 语言中用于定义常量,即其值在程序运行期间不可更改的变量。`const` 可以与多种数据类型结合使用,例如 `const int x = 10...
### const、extern、static、volatile ...通过以上的介绍可以看出,`const`、`extern`、`static` 和 `volatile` 这四个关键字在 C 和 C++ 中有着广泛的应用。正确地使用这些关键字可以极大地提高代码的质量和可维护性。
C++ 中 const 用法全解 const 在 C++ 中占有重要作用,属于小兵立大功的典型,本文档详细介绍了如何使用 const。 1. const 常量 在 C++ 中,const 可以用来修饰变量,称为常量。例如:const int max = 100; 这种...
在C++编程语言中,`const`、`volatile`和`mutable`是三个非常重要的关键字,它们用于控制变量、指针和引用的行为。本文将详细阐述这三个关键字的用途和应用场景。 首先,`const`关键字主要用于声明常量或者使得变量...
本文将深入探讨如何利用C++来实现这两种编码格式之间的转换,并讨论在转换过程中可能遇到的错误处理问题。 首先,让我们了解一下UTF-8和UTF-16的基本概念。UTF-8是一种变长的字符编码,它使用1到4个字节来表示一个...
C++语言中的const关键字是一个非常重要的修饰符,它的核心作用是声明一个变量为常量,即不可修改。正确地理解和使用const,可以帮助提高程序的健壮性和安全性。本文将详细介绍const关键字在C++中的使用方法。 一、...
C++中const、volatile、mutable使用方法小结 C++中const、volatile、mutable是三个重要的关键字,它们在日常编程中经常被使用,但是很多人对它们的理解和使用都不是很清楚。下面我们将对这三个关键字的使用方法进行...
BinTreeNode<Type> *GetLeftChild() const {return leftChild;} BinTreeNode<Type> *GetRightChild() const {return rightChild;} void SetData(const Type &d) {data=d;} void SetLeftChild(BinTreeNode ...
徐彤老师耗时三年倾心制作,专业录制,通俗,细致的讲解了C++ 编程从入门到高级
C++中的结构体和管理进程代码详解 结构体是C++中的一种自定义数据类型,可以包含多个变量和函数成员。结构体的成员可以是变量、函数或其他结构体。结构体的定义使用struct关键字,后面跟着结构体的名称和成员列表。...
徐彤老师耗时三年倾心制作,专业录制,通俗,细致的讲解了C++ 编程从入门到高级