最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
js中const对象调用const成员函数示例
时间:2016-02-02 编辑:简简单单 来源:一聚教程网
Error C2662, cannot convert ‘this’ pointer from ‘const class ’ to ‘class &’ 标签: <无>
代码片段
#pragma once
class Complex
{
public:
Complex(void);
~Complex(void);
Complex(int real,int imag);
int get_real()const;//注意const成员函数的const修饰符是必须的
int get_imag()const;
void Display()const;
void Addone(const Complex& other);
private:
int real_;
int imag_;
};
#include "Complex.h"
#include
using namespace std;
Complex::Complex(void) {}
Complex:: Complex(int real,int imag):real_(real),imag_(imag) {}
int Complex:: get_real()const//注意const成员函数的const修饰符是必须的
{ return real_;
}
int Complex:: get_imag ()const
{ return imag_;
}
void Complex::Display()const
{ cout<
//注意作为参数的other定义成是cons类型的对象,所以它只能够调用const类型的成员函数,因此以下调用到的get_real(),get_imag()成员函数必须先定义成const成员函数
void Complex::Addone(const Complex& other)
{ real_+=other.get_real();
imag_+=other.get_imag();
}
Complex::~Complex(void)
{
}
#include
#include "Complex.h"
using namespace std;
Complex Add( Complex& A, Complex& B)
{ return Complex(A.get_real()+B.get_real(),A.get_imag()+B.get_imag());
};
void main()
{ Complex a(10,5);
Complex b(11,0);
Complex c;
a.Display();
b.Display();
c = Add(a,b);
c.Display();
c.Addone(a);
c.Display();
}
相关文章
- JS回调函数基本定义与用法 05-25
- d3.js中冷门却实用的内置函数总结 03-13
- js中非常强大的光标位置工具函数 09-29
- JS函数object is not a function的bug 06-14
- JS常用的对象函数总结归纳 03-30
- 浅析js随机函数Math.random() 03-29