当前位置:网站首页>【无标题】
【无标题】
2022-04-23 05:48:00 【*陌上花开】
左值右值,柔性数组
一、右值、左值
在c中,左值就是可以被赋值的,右值就是不可被赋值的
在c11标准下:
所有的值必属于左值、右值两者之一。
右值分为纯右值和将亡值
在C++11中可以取地址的、有名字的就是左值,反之,不能取地址的、没有名字的就是右值(将亡值或纯右值)。
左值有地址,名字与生存期一致,有名字就有生存期。
右值不能取地址。
&&:右值引用,引用普通对象,纯右值(只能引用右值,也就是没有名字的)
&:左值引用,只能引用具有名字的
右值变左值,给一个名字。
```cpp
#include<iostream>
using namespace std;
#include<string>
class String
{
char* str;
public:
String(const char* p = NULL) :str(NULL)
{
if (p != NULL)
{
str = new char[strlen(p) + 1];
strcpy(str, p);
}
str = new char[1];
*str = '\0';
}
~String()
{
if (str != NULL)
{
delete[] str;
}
str = NULL;
}
String& operator=(const String& s)
{
if (this != &s)
{
delete[]str;
str = new char[strlen(s.str) + 1];
strcpy(str, s.str);
}
return *this;
}
String(String&& s)//移动构造
{
cout << "move copy construct:" << this << endl;
str = s.str;
s.str = NULL;
}
String& operator=(String&& s)//移动赋值
{
if (this != &s)
{
str = s.str;
s.str = NULL;
}
cout << this << "move operator =" << &s << endl;
return *this;
}
};
String fun()
{
String s2("zyt");
return s2;//返回s2,会生成一个将亡值,将亡值不具有名字
}
int main()
{
String s1;
s1 = fun();
return 0;
}
二、柔性数组
数组的大小声明为0,或者不给出大小,称之为柔性数组。
全局数组和局部数组不能这么定义。
struct sd_node
{
int num;
int size;
char data[];
};
//或
struct sd_node
{
int num;
int size;
char data[0];
}
版权声明
本文为[*陌上花开]所创,转载请带上原文链接,感谢
https://blog.csdn.net/swint_er/article/details/124194394
边栏推荐
- LockSupport. Park and unpark, wait and notify
- Import of data
- 基于pygame库编写的五子棋游戏
- MySQL groups are sorted by a field, and the first value is taken
- Gesture recognition research
- Substring Inversion (Easy Version)
- Completely clean up MySQL win
- selenium+PhantomJS破解滑动验证2
- [leetcode 67] sum of two binary numbers
- Rust的闭包类型(Fn, FnMut, FnOne的区别)
猜你喜欢
Type conversion in C #
定位器
[leetcode 19] delete the penultimate node of the linked list
Detection technology and principle
Basic knowledge of network in cloud computing
Substring Inversion (Easy Version)
Storing inherited knowledge in cloud computing
7-21日错题涉及知识点。
How SYSTEMd uses / etc / init D script
基于Sentinel+Nacos 对Feign Client 动态添加默认熔断规则
随机推荐
线程和进程的关系和区别是什么
Collections multiple parameter sorting
Busybox initrd and initialization process
Example of ticket selling with reentrant lock
[leetcode 59] spiral matrix II
电机与拖动(戚金清版)学习整理
Techniques et principes de détection
词频统计
Rust:如何实现一个线程池?
大学概率论与数理统计知识点详细整理
Robocode教程3——Robo机器剖析
Plane semi intersecting plate
爬虫效率提升方法
Robocode教程7——雷达锁定
Generation of verification code
Advanced operation of idea debug
Export the articles written in CSDN to PDF format
Guaba and Computational Geometry
scikit-learn sklearn 0.18 官方文档中文版
程序設計訓練