指针与const之间的组合,写了个例子温习一下
2016-11-11 19:04
351 查看
[cpp] view
plain copy
print?
#include <iostream>
class A
{
public:
A()
{
this->i = 1;
}
int get() const
{
return this->i;
};
void set(int i)
{
this->i = i;
};
private:
int i;
};
int main()
{
// 指针常量, 必须在声明时进行初始化。
A *const p = new A;
std::cout << p->get() << std::endl;
p->set(3);
// p = p + 1; 编译就报错了。
std::cout << p->get() << std::endl;
// 指向常量的指针
const A *p1 = new A;
std::cout << p1 << std::endl;
p1 = p1 + 1;
// p1->set(3); 编译就报错了。
std::cout << p1 << std::endl;
// 指向常量的常指针
const A *const p2 = new A;
std::cout << p2->get() << std::endl;
return 0;
}
总结:const修饰的是什么,什么就是常量
plain copy
print?
#include <iostream>
class A
{
public:
A()
{
this->i = 1;
}
int get() const
{
return this->i;
};
void set(int i)
{
this->i = i;
};
private:
int i;
};
int main()
{
// 指针常量, 必须在声明时进行初始化。
A *const p = new A;
std::cout << p->get() << std::endl;
p->set(3);
// p = p + 1; 编译就报错了。
std::cout << p->get() << std::endl;
// 指向常量的指针
const A *p1 = new A;
std::cout << p1 << std::endl;
p1 = p1 + 1;
// p1->set(3); 编译就报错了。
std::cout << p1 << std::endl;
// 指向常量的常指针
const A *const p2 = new A;
std::cout << p2->get() << std::endl;
return 0;
}
总结:const修饰的是什么,什么就是常量
相关文章推荐
- 指针与const之间的组合,写了个例子温习一下
- 【翠字营】:const和指针组合 分析总结
- C 语言中 const,指针,数组例子
- C++ 指针与const的组合复习
- C++ - const常量与指针和引用之间的关系
- 比较常见的const与指针的组合情况
- const和指针组合的变化
- C++ const与指针之间组合的辨析
- C++中const和指针*的组合问题
- C++ const限定符之顶层const & 底层const & 指针 & 引用之间的关系
- C++中的const和指针组合
- 初学函数指针,写个小例子来纪念一下
- const与指针之间的赋值
- C++中的const关键字系列——常类型与常指针之间的纠缠
- C++类中各种不同类型成员根据是否static 、是否const类型的初始化方法不尽相同,写代码时经常搞乱,网上搜了一下资料,全部总结一下。一、例子 [cpp] view plaincopy ---
- c,const和指针组合的几种意义
- 彩票双色球 选号 组合[温习一下排列组合的知识吧]
- const和非const之间的区别只适用于指针和引用指向的数据,也就是说fun(char)与fun(const char)将出现二义性错误
- 今天有温习了一下指针,最近遇到很多指针问题,又蒙了
- C++中引用、指针与const之间的爱恨情愁