`

C++ function after const

阅读更多
http://topic.csdn.net/t/20061205/22/5209161.html

一、函数名后跟着const的函数,不能操作data-member
二、const可修饰函数名的前、中、后,中后会重载(前貌似不会吧)

#include <iostream>
#include <string>
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   //  assignment of data-member `R::R1' in read-only structure
{
    cout << R1 + R2 << endl;
}


int main(int argc,char* argv[])
{
    R a(5,4);
    a.print();
    const R b(5,4);   //  R被const修饰了,对象只读
    b.print();

    return 0;
}

分享到:
评论

相关推荐

    Google C++ Style Guide(Google C++编程规范)高清PDF

    Other C++ Features Reference Arguments Function Overloading Default Arguments Variable-Length Arrays and alloca() Friends Exceptions Run-Time Type Information (RTTI) Casting Streams Preincrement and ...

    C++复习资料精华

    it is a parameter, indicating the address of the object, so it can only be used inside the function, and it only makes sense after the object is instantiated. 3. static_cast、reintepret_cast 和 const...

    Google C++ Style Guide_英文版.pdf

    - **Pre-increment and Pre-decrement:** Prefer pre-increment and pre-decrement over post-increment and post-decrement when the value is not needed after the operation. - **Use of const:** Use const for...

    Rad Studio Delphi C++builder XE 10.4 Patch2

    RSP-28476 LSP ErrorInsight in Structure Pane only shows one keystroke after editor RSP-28400 [BadCG] Operator Assign is not always invoked for fields RSP-28372 [Regression] Bad codegen in function ...

    cocos2d-x c++的iconv.rar

    extern iconv_t iconv_open (const char* tocode, const char* fromcode); /* Converts, using conversion descriptor ‘cd’, at most ‘*inbytesleft’ bytes starting at ‘*inbuf’, writing at most ‘*...

    c++出错提示英汉对照表

    64. **Reference to local variable 'xxx' after its lifetime** - 'xxx'生命周期结束后引用局部变量:在变量的作用域之外引用变量。确保只在变量的生命周期内引用变量。 65. **Size of array 'xxx' depends on ...

    程序源代码

    size_t hashFunction(const std::string& key) const; }; #endif // HASH_H ``` 在`hash.h`中,`hashFunction`是关键的部分,因为它将键转换为哈希值,分配到存储桶中。常见的哈希函数包括简单的模运算、DJB2哈希...

    stdafx.h代码

    stdafx.h的代码// This is a part of the Microsoft Foundation Classes C++ library. // Copyright (C) 1992-1998 Microsoft Corporation // All rights reserved. // // This source code is only intended as a ...

    图像处理图像灰度增强

    // Microsoft Visual C++ will insert additional declarations immediately before the previous line. #endif // !defined(AFX_FIGURE_H__2830E4B2_3CD3_4E32_AD84_273816B535D0__INCLUDED_)

    C++MFC教程

    Visual C++MFC入门教程 目录 +-- 第一章 VC入门 |------ 1.1 如何学好VC |------ 1.2 理解Windows消息机制 |------ 1.3 利用Visual C++/MFC开发Windows程序的优势 |------ 1.4 利用MFC进行开发的通用方法介绍 |----...

    freeborders福瑞博德笔试题(1)--2009年

    在第三个问题中,程序员需要判断抽象类的定义:抽象类是一种类,它 declare with the keyword abstract(has at least one virtual function)。 数组和指针的关系 在C++中,数组和指针有着紧密的关系。数组可以隐式...

    vc++回调函数的用法

    void DLLDIR __stdcall Bubblesort(byte* array, int size, int elem_size, CompareFunction cmpFunc) { for (int i = 0; i ; i++) { for (int j = 0; j ; j++) { // 比较元素 if (1 == (*cmpFunc)(array + j * ...

    FlexGraphics_V_1.79_D4-XE10.2_Downloadly.ir

    - FIX: After deleting the selected points in the TFlexPanel.DeleteSelectedPoints the all figures with one point also deleted. - FIX: When the curve contain more then one figure and they were all ...

    一个跨平台的CString源码

    // necessary after I rewrote the code as a template. Others helped me // improve the CString facade. // // Anyway, these people are (in chronological order): // // - Pete the Plumber (???) // -...

    CheckMem.pas

    * Go for the biggest classes first - if you see a TMyFom &lt;class&gt; leaking, try to fix this one first instead of going after a tiny TFont class. Since a Form will usually contain a lot of other classes,...

    C PROGRAMMING TUTORIAL

    Used to retain the value of a variable even after the end of the block. - **Lifetime:** Until the end of the program. - **Scope:** Limited to the current block. ##### The extern Storage Class Used ...

    uCOS-II v2.52 在 STM32 上的移植

    3.After several days researching about hard SPI to drive LCD, failed. So switch to use a faster GPIO method, as below: Replace "#define LCD_RST_H() GPIO_SetBits(LCD_CTRL_PORT, LCD_RST)" in ST ...

    EurekaLog_7.5.0.0_Enterprise

    32)..Fixed: Memory leak after low-level unhook of function 33)..Fixed: Re-parenting after ReallocMem 34)..Fixed: Editing SMTP server options 35)..Fixed: SMTP server not using real user...

    au3反编译源码

    Is 'Function Renamer' to transfer the function names from one simulare file to your decompiled au3-file. A simulare file can be a included 'include files' but can be also an older version of the ...

    MFC的程序框架剖析

    们之间的纽带仅仅在于这个C++窗口类内部的成员变量m_hWnd,该变量保存了与这个C++窗口类对象相关的哪个窗口 的句柄 (3)但是,当C++窗口类对象销毁时,与之相关的窗口也将销毁,因为它们之间的纽带m_hWnd已经断了 3、...

Global site tag (gtag.js) - Google Analytics