-
std::string 是 C++ 中用来存放字符串的类型
查看全部 -
1
查看全部 -
函数指针的声明方法为:
返回值类型 ( * 指针变量名) ([形参列表]);
注1:“返回值类型”说明函数的返回类型,“(指针变量名 )”中的括号不能省,括号改变了运算符的优先级。若省略整体则成为一个函数说明,说明了一个返回的数据类型是指针的函数,后面的“形参列表”表示指针变量指向的函数所带的参数列表。例如:
int func(int x); /* 声明一个函数 */
int (*f) (int x); /* 声明一个函数指针 */
f=func; /* 将func函数的首地址赋给指针f */
或者使用下面的方法将函数地址赋给函数指针:
f = &func;
查看全部 -
普通变量使用.
指针变量使用->
查看全部 -
#include<iostream>
using namespace std;
class Mat {
friend ostream& operator << (ostream& out, const Mat& m);
public:
int row = 0;
int col = 0;
float * * mat = nullptr;
private:
void init(int row, int col)
{
if (row && col) {
mat = new float*[row];
for (int i = 0; i < row; i++) {
mat[i] = new float[col];
for (int j = 0; j < col; j++){
mat[i][j] = 0;
if(i == j){
mat[i][j] = 2;
}
}
}
}
}
public:
Mat(int row = 0, int col = 0)
{
this->row = row;
this->col = col;
init(row, col);
}
Mat(const Mat &m)
{
this->row = m.row;
this->col = m.col;
init(row, col);
for (int i = 0; i < row; i++){
for (int j = 0; j < col; j++){
mat[i][j] = m.mat[i][j];
}
}
}
~Mat()
{
if (mat != nullptr) {
for (int i = 0; i < row; i++){
if (mat[i]) {
delete[] mat[i];
mat[i] = nullptr;
}
}
if (mat){
delete[] mat;
}
mat = nullptr;
}
}
Mat & operator = (const Mat &m)
{
if (mat != nullptr) {
for (int i = 0; i < row; i++){
if (mat[i]) {
delete[] mat[i];
mat[i] = nullptr;
}
}
if (mat){
delete[] mat;
}
mat = nullptr;
}
row = m.row;
col = m.col;
init(row, col);
for (int i = 0; i < row; i++){
for (int j = 0; j < col; j++){
mat[i][j] = m.mat[i][j];
}
}
return *this;
}
Mat operator * (const Mat &m)
{
Mat res(row, m.col);
for (int i = 0; i < res.row; i++) {
for (int j = 0; j < res.col; j++) {
res.mat[i][j] = 0.0f;
}
}
if (m.row != col){
}
else {
for (int i = 0; i < res.row; i++) {
for (int j = 0; j < res.col; j++) {
for (int k = 0; k < res.row; k++) {
res.mat[i][j] += mat[i][k] * m.mat[k][j];
}
}
}
}
return res;
}
};
ostream& operator << (ostream& out, const Mat& m)
{
for(int i = 0; i < m.row; i ++) {
for(int j = 0; j < m.col; j ++) {
cout << m.mat[i][j] << ' ';
}
cout << endl;
}
return out;
}
int main()
{
Mat ma(3, 3);
Mat mb(3, 3);
Mat res = ma * mb;
cout << res << endl;
return 0;
}
查看全部 -
从函数中取出一个对象
理解:函数的返回值是一个对象啊
通过把返回值写在参数里面,怎么实现返回的是对象呢?
查看全部 -
1、引用可以认为是变量的别名**
2、引用和指针在编译器的汇编中是一样的,因此他们的功能很类似
3、用指针操作对象时,用的是 箭头操作符
在操作普通的变量时,用的是 点操作符
**而且,引用可以用的是 点操作符
4、引用必须赋值
查看全部 -
class S { int a =20; //1const返回类型为常量;2const要使用成员函数为const的成员 const int & aaa() const { return a; } }
查看全部 -
memcpy指的是C和C++使用的内存拷贝函数,函数原型为void *memcpy(void *destin, void *source, unsigned n);函数的功能是从源内存地址的起始位置开始拷贝若干个字节到目标内存地址中,即从源source中拷贝n个字节到目标destin中。
查看全部 -
析构函数是用“~”定义的,若不定义自动生成,若手动定义则不再生成,可用于善后工作
实例化的时候会调用构造函数,main函数结束时销毁再调用析构函数
查看全部 -
回调函数、钩子函数
查看全部 -
指正指向函数
查看全部 -
Staff(const Staff & staff);//k拷贝一份类中某函数地址
查看全部 -
*在不同地方的含义
1、在定义指针中时,*为指针定义符
2、在可执行语句指针之前,*为指针引用符
3、别忘了还有运算符的作用
&在不同地方的含义
1、在定义变量中,&为引用定义符,且必须初始化引用。
2、在可执行语句中,&为取址符
如以上描述有误,欢迎批评指针!!!
查看全部 -
内存管理
资源获得初始化原则(RAII)
构造函数 创建资源
析构函数 销毁资源
查看全部
举报