左值右值,柔性数组


一、右值、左值

在c中,左值就是可以被赋值的,右值就是不可被赋值的
在c11标准下:
所有的值必属于左值、右值两者之一。
右值分为纯右值将亡值
在C++11中可以取地址的、有名字的就是左值,反之,不能取地址的、没有名字的就是右值(将亡值或纯右值)。
左值有地址,名字与生存期一致,有名字就有生存期。
右值不能取地址。
&&:右值引用,引用普通对象,纯右值(只能引用右值,也就是没有名字的)
&:左值引用,只能引用具有名字的
右值变左值,给一个名字。
在这里插入图片描述

在这里插入图片描述

#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];
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值