开发者社区> 吴英强> 正文
阿里云
为了无法计算的价值
打开APP
阿里云APP内打开

【C/C++学院】0820-Nullptr/const对象/类指针引用以及mallocfree与newde/类重载运算符/QT加法重载/类的重载赋值运算/自增在前在后差别/赋值重载深浅拷贝/重载下标

简介: <p></p> <p></p> <h2> <span style="font-family:宋体; font-size:16pt">Nullptr</span><span style="font-family:宋体; font-size:16pt"></span> </h2> <pre name="code" class="java">#include&lt;iostream&gt;
+关注继续查看

Nullptr

#include<iostream>

void go(int num)
{
	std::cout << "gonum" << std::endl;
}

void  go(void *p)
{
	std::cout << "gop" << std::endl;
}

void main()
{
	//void *p = nullptr;  
	void *p = NULL;//C++是强类型,严格的类型检查
	//go(p);//根据类型来处理,go(p)  gop
	go(NULL);//造成歧义,C++用Nullptr,gonum
	go(nullptr);//C++的空指针,类型

	std::cin.get();
}

const对象

#include<iostream>

class  area
{
public:
	int x;
	int y;
	mutable int  z;//不受const约束的类成员
	area() :x(10), y(10)
	{

	}
	void printxy() const
	{
		z = z + 1;
		std::cout << x << "  " << y << "\n";
	}
	void add(int a)
	{
		x += a;
		y -= a;
	}
	void go() const
	{

	}
protected:
private:
};

void main()
{
	{
		//const area *p1;
		//area const *p2;
		//const area * const p3;
		//area *const p4;
	}
	//const对象不可以引用非const成员函数
	//不可以改变内部变量,mutable例外
	const area * const p = new area;
	p->go();
	//p->add();
	//p = new area;//指针可以改变

	const area area1;
	area1.printxy();
	//area1.add(1);
	area1.go();
	//area1.x = 10;
	area1.z += 1;
	std::cin.get();
}

类指针引用以及mallocfreenewdelete差别

#include<iostream>
#include <stdlib.h>

class myclass
{
public:
	int x;
	int y;
public:
	myclass(int a, int b) :x(a), y(b)
	{
		std::cout << "构造哦" << std::endl;
	}
	myclass()
	{

	}
	~myclass()
	{
		std::cout << "销毁哦" << std::endl;//
	}

public:
    void printxy();
protected:
private:
};

void myclass::printxy()
{
	std::cout << x << "  " << y << std::endl;
}

myclass class1(10, 11);//全局变量优先main函数
myclass class2(11, 12);

void change1(myclass **pp)
{
	*pp = &class2;
}

void change2(myclass * &p)
{
	p = &class1;
}

void  main22()
{
	myclass *p=&class1;
	p->printxy();
	change1(&p);
	p->printxy();
	change2(p);
	p->printxy();

	std::cin.get();
}


void main11()
{
	//myclass *p = new myclass;
	myclass *p(new myclass(10,9));//构造函数初始化
	p->printxy();
	myclass  class1(20,1);
	myclass ** pp = &p;//二级指针存储一级指针的地址
	(*pp)->printxy();//一级类指针
	(**pp).printxy();//0级类指针
	
    std::cin.get();
}

void main122()
{
	//myclass *p = (myclass *)malloc(sizeof(myclass));
	//free(p);
	myclass *p = new myclass;
	delete p;

	std::cin.get();
}
#include <iostream>
#include<stdlib.h>

class myclassA
{
public:
	myclassA()
	{
		std::cout << "create\n";
	}
	~myclassA()
	{
		std::cout << "delete\n";
	}
protected:
private:
};

void main()
{
	//new delete自动调用构造析构
	myclassA *p = new myclassA;
	delete p;

	//只会分配内存,释放内存,不会对内存进行操作
	myclassA *p1 = (myclassA *)malloc(sizeof(myclassA));
    free(p1);

	std::cin.get();
}

类重载运算符

#include <iostream>
using namespace std;

class  mycomplex
{
public:
	//友元,需要操作类的内部
	//ostream 引用标准输入输出流,
	friend ostream & operator <<(ostream & out, mycomplex & Complex);
	friend istream & operator >>(istream & in, mycomplex & Complex);
	friend   mycomplex  operator +(mycomplex adddata1, mycomplex adddata2);
	friend   mycomplex  operator +(mycomplex adddata1, int x);
//友元函数可以处理不同的类型交错
	//成员函数能实现,友元函数都可以实现
	//友元函数
	int x;
	int y;//x,y坐标
	//没有构造无法使用this,初始化
	mycomplex()
	{
		this->x = 0;
		this->y = 0;
	}
	mycomplex(int x, int y) :x(x),y(y)
	{
		//this->x += 1;
		//this->y += 1;
	}
	~mycomplex()
	{

	}
	void show()
	{
		std::cout << x << "+" << y << "i" << std::endl;
	}
	void operator ++()
	{
		this->x++;
		this->y++;

	}
	void  operator --();
    
	int operator ()(int num)//重载函数调用运算符,变量名可以当作一个函数调用参数,返回结果
	{

		cout << num << endl;
		return num + num;
	}
	/*
	mycomplex  operator +(mycomplex adddata)
	{
		mycomplex temp;
		temp.x = this->x + adddata.x;
		temp.y = this->y + adddata.y;
		return temp;
	}
	*/	

protected:

private:

};
void  mycomplex::operator--()
{
	this->x--;
	this->y--;
}
//输入输出,cout,屏幕,fout文件
ostream & operator <<(ostream & out, mycomplex & Complex)
{
	out << Complex.x << "+" << Complex.y << "i" << std::endl;
	return out;
}
istream & operator >>(istream & in, mycomplex & Complex)
{
	cout << "请输入X,Y" << endl;
	in >> Complex.x >> Complex.y;
	return in;
}
mycomplex  operator +(mycomplex adddata1  ,mycomplex adddata2)
{
	mycomplex temp;
	temp.x = adddata1.x + adddata2.x;
	temp.y = adddata1.y + adddata2.y;
	return temp;
}
mycomplex  operator +(mycomplex adddata1, int x)
{
	mycomplex temp;
	temp.x = adddata1.x + x;
	temp.y = adddata1.y + x;
	return temp;
}

//++,--先自增,先自减

//

void main()
{
	mycomplex my1(7, 8), my2(9, 10);
	std::cout << my1 + my2 << std::endl;
	std::cout << my1 + 3 << std::endl;
	std::cin.get();

}

void main2()
{
	mycomplex my1;
	cin >> my1;
	cout << my1;
	//my1++;
	++my1;
	cout << my1;
	my1--;
	cout << my1;
	std::cout<<my1(1)<<std::endl;
	std::cout << my1(2) << std::endl;

	std::cin.get();
	std::cin.get();

}

void main1111()
{
	mycomplex my1(7, 8),my2(9,10);
	//my1.show();
	//my2.show();
	std::cout << my1;
	std::cout << my2;
	int num;
	std::cin >> num;
	//std::cin >> my1;

	std::cin.get();
}

类的重载赋值运算复合赋值运算关系运算元重载

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include<QLabel>

namespace Ui {
class Dialog;
}

class mylabel
{
public:

     QLabel *ql;
     int cx;
     int cy;
     mylabel()
     {
         ql=new QLabel("12345") ;
         cx=cy=300;
         ql->move(cx,cy);//移动位置
     }
     ~mylabel()
     {
        delete ql;
     }
    void  show()
    {
       ql->show();
    }

};

class Dialog : public QDialog
{
    Q_OBJECT

public:
    int x;//长 ,宽
    int y;
    int cx;//位置,x,y
    int cy;
public:

    explicit Dialog(QWidget *parent = 0);
    friend void  operator +=(Dialog & d  , mylabel &my  );

    ~Dialog();
    void operator ++()
    {
        this->cx++;
        this->cy++;
        this->move(cx,cy);
    }
   void setxy()
   {
       this->resize(x,y);
   }
   void settoxy(int a,int b)
   {
       this->x=a;
       this->y=b;
   }

   //二元运算符
   Dialog * operator +(Dialog const &adddata)
   {
         Dialog *p=new Dialog;
         p->x=this->x+adddata.x;
         p->y=this->y+adddata.y;
         return p;
   }
   Dialog *operator =(Dialog const &setdata)
   {
       this->x=setdata.x;
       this->y=setdata.y;
       this->setxy();
       return this;
   }
   //int a,int b, a+=b
   //Dialog a,int b,a+=b
   Dialog *operator +=(int length)
   {
       this->x+=length;
       this->y+=length;
       this->setxy();
       return this;
   }
   bool operator <(Dialog const &data)
   {
       return (this->x*this->y) < (data.x*data.y);
   }

private:
    Ui::Dialog *ui;
};

#endif // DIALOG_H
#include "dialog.h"
#include "ui_dialog.h"

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
    x=y=300;
    this->resize(x,y);
    cx=cy=0;
    this->move(cx,cy);
}

Dialog::~Dialog()
{
    delete ui;
}

void  operator +=(Dialog & d  , mylabel &my  )
{
      d.cx+=my.cx;
      d.cy+=my.cy;
      d.move(d.cx,d.cy);
}
#include "dialog.h"
#include <QApplication>
#include<Windows.h>
#include<QDebug>

void   add(Dialog &add1,Dialog &add2)
{
    Dialog temp;//栈上,用完了马上回收
    temp.x=add1.x+add2.x;
    temp.y=add1.x+add2.y ;
    temp.show();
    temp.setxy();
     Sleep(2000);
   //return 副本机制  return temp;

}
Dialog *  addX(Dialog &add1,Dialog &add2)
{
    Dialog *p=new Dialog;//堆上,只有自己回收才行
    p->x=add1.x+add2.x;
    p->y=add1.x+add2.y ;
    p->show();
    p->setxy();
    return p;
}

// >
// =
//+=
//友元重载+=,= 不同类的对象的位置
template<class T>
void showall(T* myt)
{
    myt->show();
}

//友元函数访问私有变量
//如果重载的时候,不用到私有变量,不需要友元
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);//框架支持

   // QLabel *ql=new QLabel("12345");
   // ql->show();
   // showall(ql);
     mylabel  label1;
     showall(&label1);


    Dialog w1;
    //w1.show();
    showall(&w1);
    w1+=label1;

    return a.exec();
}

int mainC(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Dialog w1;
    w1.show();
    w1.settoxy(400,700);
    w1.setxy();
    Dialog w2;
    w2.show();
    w2.settoxy(700,400);
    w2.setxy();
    w1=w2;
    w1+=230;
    qDebug()<<(w1<w2);
    qDebug()<<(w2<w1);

    return a.exec();
}

int mainB(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Dialog w1;
    w1.show();
    Dialog w2;
    w2.show();
   // addX(w1,w2);
    Dialog *p=w1+w2;
    p->setxy();
    p->show();

    return a.exec();
}

int mainA(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Dialog w;
    w.show();
    w.move(0,0);
    for(int i=0;i<800;i++)
    {
        w++;
    }

    return a.exec();
}

自增在前在后差别

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>
#include<QString>

namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{
    Q_OBJECT

public:
    explicit Dialog(QWidget *parent = 0);
    ~Dialog();
    void setxy(int a,int b)
    {
        this->x=a;
        this->y=b;
    }
    Dialog  *operator ++()
    {
        this->x += 1;
        this->y += 1;
        return this;
    }
    Dialog  *operator ++(int data)
    {
       Dialog *ptemp=new Dialog;
       ptemp->x=this->x;
       ptemp->y=this->y;

       this->x+=1;
       this->y+=1;

       return ptemp;
    }

private slots:
    void on_pushButton_clicked();
    void on_pushButton_2_clicked();
    void on_pushButton_3_clicked();

private:
    Ui::Dialog *ui;
    int x;
    int y;
};

#endif // DIALOG_H
#include "dialog.h"
#include "ui_dialog.h"

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
    this->x=10;
    this->y=100;

    QString str1,str2;
    str1.setNum(this->x);
    str2.setNum(this->y);
    ui->lineEdit->setText(str1);
    ui->lineEdit_2->setText(str2);
}

Dialog::~Dialog()
{
    delete ui;
}

void Dialog::on_pushButton_clicked()
{
    QString str1,str2;
    str1.setNum(this->x);
    str2.setNum(this->y);
    ui->lineEdit->setText(str1);
    ui->lineEdit_2->setText(str2);
}

void Dialog::on_pushButton_2_clicked()
{
      ++(*this);
}

void Dialog::on_pushButton_3_clicked()
{
    Dialog *p=  (*this)++;
    p->show();
}
#include "dialog.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Dialog w;
    w.show();
    //w.setxy(10,20);
    //++w;
   // Dialog *p=w++;
   // p->show();

    return a.exec();
}

赋值重载深浅拷贝

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->resize(100,100);
}

MainWindow::~MainWindow()
{
    delete ui;
}
#include "mainwindow.h"
#include <QApplication>
#include<QDebug>


class morewindows
{
public:
    MainWindow *p[5];//指针数组,每一个元素是指针
    morewindows()
    {
        for(int i=0;i<5;i++)
        {
            p[i]=new MainWindow;
            p[i]->show();
            p[i]->move(i*100,i*100);
        }
    }
    ~morewindows()
    {
        for(int i=0;i<5;i++)
        {
           delete p[i];

        }
    }
};

class morewindowss
{
public:
    MainWindow *p[5][4];

    morewindowss()
    {
        for(int i=0;i<5;i++ )
        {
            for(int j=0;j<4;j++)
            {
                p[i][j]=new MainWindow;
                p[i][j]->show();
                p[i][j]->move(i*130,j*130);
            }
        }
    }
    ~morewindowss()
    {
        for(int i=0;i<5;i++ )
        {
            for(int j=0;j<4;j++)
            {
              delete p[i][j];
            }

        }
    }
};
//int a[5]   int *p=a;   int *p= new int[5];
//int *a[5]   int **p=a  int **p=new  (int *)[5];
//int  *a[3][5]
//int * (*p)[5]

class morewindowsss
{
public:
    MainWindow  **p;//二级指针
    morewindowsss()
    {
       // p= (MainWindow **)malloc(sizeof(MainWindow *)*5);
    }
    void init(int num)
    {
        p= new MainWindow * [5];//new的时候类型不需要加上括号
        for(int i=0;i<5;i++)
        {
            p[i]=new MainWindow;
            p[i]->show();
            p[i]->move(num*100,i*100);
        }
    }

    void move(int x,int y)
    {
        for(int i=0;i<5;i++)
        {
            p[i]->move(x*100,y*100);
        }
    }
    morewindowsss  & operator = (morewindowsss const & more)//自己写的深拷贝
    {
        qDebug()<<"shen";
        this->p = new MainWindow * [5];

        for(int i=0;i<5;i++)
        {
            p[i]=new MainWindow;
            p[i]->show();
            p[i]->move(500,i*100);

        }
        return *this;
    }


    ~morewindowsss()
    {
        for(int i=0;i<5;i++)
        {
           delete p[i];
        }
        delete [] p;
       // free(p);
    }

};

class morewindowssss
{
public:
   // MainWindow *p[5][4];//二维数组,每一个元素都是指针
    MainWindow  **pA;//二级指针
    MainWindow *(*p)[4];//指向二维指针数组的指针
    morewindowssss()
    {
        pA=new  MainWindow  *[20];//一维数组
        p=(MainWindow *(*)[4])  pA;

        for(int i=0;i<5;i++ )
        {
            for(int j=0;j<4;j++)
            {
                p[i][j]=new MainWindow;
                p[i][j]->show();
                p[i][j]->move(i*130,j*130);
            }

        }
    }
    ~morewindowssss()
    {
        for(int i=0;i<5;i++ )
        {
            for(int j=0;j<4;j++)
            {
              delete p[i][j];
            }
        }

        delete [] pA;
    }
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    morewindowsss   more1;
    more1.init(1);
   // more1.move(1,2);

   morewindowsss   more2;//拷贝构造

    more2=more1;//赋值

   // morewindowsss   more2(more1);
    more2.move(3,4);

    return a.exec();
}

重载下标

#include "mainwindow.h"
#include <QApplication>

class morewindow
{
public:
     MainWindow *p[5];
     morewindow()
     {
         for(int i=0;i<5;i++)
         {
             p[i]=new MainWindow;
             p[i]->show();
             p[i]->move(i*100,i*100);

         }
     }
     ~morewindow()
     {
         for(int i=0;i<5;i++)
         {
            delete p[i];
         }
     }
     MainWindow * operator [](int i)
     {
         return p[i];
     }
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

     morewindow   more1;
     more1[3]->hide();//重载下标

    return a.exec();
}



















版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
SAP QM 执行事务代码QS51维护使用决策的选择集,系统报错 – Transaction no longer valid for catalog ‘3’ -
SAP QM 执行事务代码QS51维护使用决策的选择集,系统报错 – Transaction no longer valid for catalog ‘3’ -
50 0
30行Python代码从百度自动下载图片(内附源码和exe程序)
你还在为批量下载表情包发愁吗? 你还在为找不到好看的图片素材在担忧吗? 你还在为各种精美壁纸的挑选和下载找遍各大网站吗?
63 0
Swift UI对Flutter的意义;JSConf 2019归来记;未来属于声明式编程丨体验科技精选第 4 期
这里是蚂蚁金服体验科技精选 第 4 期,本期内容包括原创精选、蚂蚁前端动态和行业动态,内涵福利。
5199 0
零元学Expression Design 4 - Chapter 7 使用内建功能「Clone」来达成Path的影分身之术
原文:零元学Expression Design 4 - Chapter 7 使用内建功能「Clone」来达成Path的影分身之术 本章所介绍的是便利且快速的内建工具Clone ? 本章所介绍的是便利且快速的内建工具Clone ? ? 为什麽会说像是影分身之术呢? ? 请参照火影忍者(NARUTO): 《分身术》会分身术者,能以一身分出几身,几十身,乃至千百身。
1060 0
ET Brain: Exploring New Uses for Data and AI
This whitepaper seeks to introduce the various ET Brain solutions, their features, and how the solutions are designed to transform the respective target industries.
1521 0
零元学Expression Blend 4 - Chapter 5 2.5D转换的使用技巧
原文:零元学Expression Blend 4 - Chapter 5 2.5D转换的使用技巧 本章将延续上篇零元学Expression Blend4 - Chapter 4元件重复运用的观念所制作的图来进行,教你如何将图做X、Y、Z空间的延伸。
1180 0
Study Notes Weekly No.4(tracker & csi & execute C# from XSLT file)
本文讲的是Study Notes Weekly No.4(tracker & csi & execute C# from XSLT file),About: use tracker to load dll use csi to bypass Application Whitelisting ...
1551 0
一个很简单的淘宝优惠券搜索助手 大家看看有没有用吧 下载地址:http://pan.baidu.com/s/1skRHTDF
一个很简单的淘宝优惠券搜索助手 大家看看有没有用吧 下载地址:http://pan.baidu.com/s/1skRHTDF
1951 0
使用JQUERY的flexselect插件来实现将SELECT下拉菜单变成自动补全输入框
这也是下拉列表太长了之后,使用的同事提出来的意见, 然后,本来开始想将DJANGO的那个后台下拉菜单移植过来的,但发现不现实,也麻烦, 就找了几个JQUERY的插件测试了一下,最后选中了flexselect。
1310 0
+关注
329
文章
0
问答
文章排行榜
最热
最新
相关电子书
更多
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
冬季实战营第三期:MySQL数据库进阶实战
立即下载