文章目录
  1. 1. 重载运算符
  2. 2. 多维数组
  3. 3. ->(指向结构体成员运算符)
  4. 4. new
  5. 5. AGM_SZ7A代码memo
  6. 6. 疑问

数组、->(指向结构体成员运算符)newvector
哪咤

重载运算符

重载的运算符是具有特殊名字的函数:它们的名字由关键字operator和其后要定义的运算符共同组成。和其他函数一样,重载的运算符也包含返回类型、参数列表以及函数体;

  1. operator
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    #include<iostream>
    int main()
    {
    struct absInt {
    int operator()(int val)const {
    return val < 0 ? -val: val;
    }
    };


    int i = -42;
    absInt absObj;
    int ui = absObj(i);

    std::cout << ui << std::endl;

    }

多维数组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream>
#include<stdio.h>
using namespace std;
void main()
{
int *p[3];
int a[3][4] = { {0,1,3,4},{5,6,7,8},{9,10,11,12} };

for (int i = 0; i < 3; i++)
{
printf("*a[%d]: %d\n",i, *a[i]); /*多维数组,如果下标只有一个下标,那应该是行下标*/
printf("&a[%d]: %d\n", i,&a[i]);
printf("a[%d]: %d\n", i,a[i]);
}
printf("\n*************************************\n");
printf("a[1][2]: %d\n", a[1][2]);
//printf("*a[1][2]: %d\n", *a[1][2]); /*illegal indirection*/
}

运行结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
*a[0]:  0
&a[0]: 12385784
a[0]: 12385784
*a[1]: 5
&a[1]: 12385800
a[1]: 12385800
*a[2]: 9
&a[2]: 12385816
a[2]: 12385816

*************************************
a[1][2]: 7
Press any key to continue . . .

->(指向结构体成员运算符)

->运算符叫做“指向结构体成员运算符”,是C语言和C++语言的一个运算符。
  一个指针当用来指向一个结构体、对象时,称之为结构体指针或对象指针。结构体指针或对象指针中的值是所指向的结构体或对象的首地址。通过结构体指针或对象指针即可访问该结构体或对象。
这需要用到结构体关键字struct和C++类关键字class。
  结构体指针和对象指针定义的形式:

1
2
3
4
struct 结构体类型名 *指针名;//结构体指针
struct 结构体类型名 *指针名 = &一个结构体的名字;//结构体指针并赋初值
struct 结构体类型名 *指针名 = new struct 结构体类型名;//结构体指针并用new申请内存
struct 结构体类型名 *指针名 =(struct 结构体类型名 *)malloc(sizeof(struct 结构体类型名))//结构体指针并用malloc申请内存 使用应包含头文件stdlib.h

用法一:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
using namespace std;
class C
{
public:
int num;
string name;
};

int main(void)
{
C obj;
C *p = &obj;
p->num = 5;
p->name = "Tony";
cout << p->num << p->name << endl;
return 0;
}

用法二:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
using namespace std;
class C
{
public:
int num;
string name;
};
void changevalue(C *t, int num, string name)
{
t->num = num;
t->name = name;
}

int main(void)
{
C obj;
changevalue(&obj, 5, "Tony");
cout << obj.num << obj.name << endl;
return 0;
}

程序输出:5Tony

new

使用new运算符分配内存后,将会返回被分配的内存地址,如果分配失败(如没有足够的内存空间)时返回0;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
using namespace std;
void main()
{
float(*cp)[5];
int i, j;
cp = new float[3][5];
for (i = 0; i < 3; i++)
for (j = 0; j < 5; j++)
*(*(cp + i) + j) = float(i * 2 + j); /*注意: *(*(cp + i) + j) 等同于 cp[i][j] 可参考: http://www.cnblogs.com/mq0036/p/3382732.html */
for (i = 0; i < 3; i++)
{
for (j = 0; j < 5; j++)
{
cout << cp[i][j] << " ";
}
cout << endl;
}
delete[] cp;
}

AGM_SZ7A代码memo

1.
boost::shared_ptr :这个环境在windows怎么配?

  1. sprintf
    函数原型:
    1
    int sprintf ( char * str, const char * format, ... );
    将 const char按printf输出格式写入 char str里,函数会根据const char*字符数;

[可参考cplusplus.com]

可参考cplusplus.com

Example代码:

1
2
3
4
5
6
7
8
9
10
11
/* sprintf example */
#include <stdio.h>

int main()
{
char buffer[50];
int n, a = 5, b = 3;
n = sprintf(buffer, "%d plus %d is %d", a, b, a + b);
printf("[%s] is a string %d chars long\n", buffer, n);
return 0;
}

Output:

1
[5 plus 3 is 8] is a string 13 chars long
  1. 容器

标准库类型vector表示对象的集合,其中所有对象的类型都相同。集合中的每个对象都有一个与之对应的索引,索引用于访问对象。因为vector容纳着其他对象,所以它也称作为容器(container)。
使用vector必须包含适当的头文件和声明。

1
2
#include<vector>
using std::vector;

vector是一个类模版。模版本身不是类或函数,相反可以将模版看作为编译器生成类或函数编写的一份说明。编译器根据模版创建类或函数的股从恒称为实例化(instantiation)。当使用模版时,需要指出编译器把类或函数实例化何种类型。

  • 在模版名字跟一对< >在尖括号放上信息
    例如:
    1
    2
    3
    vector<int>ivec;    // ivec保存int类型的对象
    vector<Sales_item> Sales_vec; //保存Sales_item类型对象
    vector<vector<string>> file; //该向量的元素是vector对象

元素的输入及访问:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> a(10, 0); //大小为10初值为0的向量a
//对其中部分元素进行输入
cin >> a[2];
cin >> a[5];
cin >> a[6];

//全部输出
int i;
for (i = 0; i<a.size(); i++)
cout << a[i] << " ";

return 0;
}

【output】

1
2
100 5 3
0 0 100 0 0 5 3 0 0 0 Press any key to continue . . .

**基本概念**

  1. 动态内存与智能指针
    为了更容易(同时也更安全)地使用动态内存,新的标准库提供了2种智能指针(smart pointer)类型来管理动态对象。智能指针的行为类似常规指针,**重要的区别是它负责自动释放所指对象**。
  • shared_ptr允许多个指针指向同一个对象。
  • unique_ptr则独占所指对象。
  • shared_ptr

类似于vector,只能指针也是模版;与vector一样,我们在< >内给出类型,之后是所定义这种智能指针的名字:

1
2
shared_ptr<string>p1;   		//shared_ptr,可以指向string
shared_ptr<list<int>>p2; //shared_ptr,可以指向int 的list
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <string>
#include <iostream>
#include <boost/shared_ptr.hpp>

class implementation
{
public:
~implementation() { std::cout <<"destroying implementation\n"; }
void do_something() { std::cout << "did something\n"; }
};

void test()
{
boost::shared_ptr<implementation> sp1(new implementation());
std::cout<<"The Sample now has "<<sp1.use_count()<<" references\n";

boost::shared_ptr<implementation> sp2 = sp1;
std::cout<<"The Sample now has "<<sp2.use_count()<<" references\n";
std::cout<<"Befor sp1.reset(); "<<sp1.use_count()<<" references\n";


sp1.reset();
std::cout<<"After Reset sp1. sp2.user_count is"<<sp2.use_count()<<" references\n";

std::cout<<"After Reset sp1. sp1.use_count is(): "<<sp1.use_count()<<" references\n";

sp2.reset();
std::cout<<std::endl<<"*****************************************"<<std::endl;

std::cout<<"After Reset sp2.||||sp2.use_count is "<<sp2.use_count()<<" references\n";

}

int main()
{
test();
}

【OutPut】

1
2
3
4
5
6
7
8
9
The Sample now has 1 references
The Sample now has 2 references
Befor sp1.reset(); 2 references
After Reset sp1. sp2.user_count is1 references
After Reset sp1. sp1.use_count is(): 0 references
destroying implementation

*****************************************
After Reset sp2.||||sp2.use_count is 0 references

可以看到,boost::shared_ptr指针sp1和sp2同时拥有了implementation对象的访问权限,且当sp1和sp2都释放对该对象的所有权时,其所管理的的对象的内存才被自动释放。在共享对象的访问权限同时,也实现了其内存的自动管理

可参考博文1.

  1. static_pointer_cast ??

出处

1
i_GUIObject = boost::static_pointer_cast<IGUIImpl>(m_OMainProcess->m_PtrGUIModule);    //take: i_GUIObject是 `IGUIImpl`的智能 指针?
  1. static_pointer_cast???强制转换???

  2. virtual int ??

    1
    2
       //初始化函数
    virtual int initialization(int argc, char *argv[]) = 0;

疑问

  1. sizeof(结构体)?

出处:

1
memset(&iOperationStatus, 0x00, sizeof(stGUIStatus));
  1. 磁盘阵列
文章目录
  1. 1. 重载运算符
  2. 2. 多维数组
  3. 3. ->(指向结构体成员运算符)
  4. 4. new
  5. 5. AGM_SZ7A代码memo
  6. 6. 疑问