object and ptr

1,

#include <iostream>
#include <string>
#include <vector>
#include <memory>

using namespace std;

class fruit {
    public:
        int *idx ;
};
int prodoucer(vector<fruit> &xc);
int prodoucer(vector<fruit> &xc) {
    int first=1;
    int second=2;
    fruit apple; 
    apple.idx = &first;
    fruit pear; 
    pear.idx = &second;
    xc.push_back(apple);
    xc.push_back(pear);
}
int main(int argc, char *argv[]) {
    vector<fruit> vi;
    int rlt = prodoucer(vi);
    cout << " out side the fun" << endl;
    cout << &vi[0] << " using " << *(vi[0].idx) << endl;//not 1
    printf("-------------\n");
    cout << &vi[1] << " using " << *(vi[1].idx) << endl;// not 2
    return 0;
}

result

 out side the fun
0x5560736e90 using -998566032
-------------
0x5560736e98 using 127

 cv::Mat:

Mat类的构成
构造函数
Mat的构造函数用来实例化一个mat对象,因此需要给出相应的参数完成初始化就可以了。mat类给出的构造函数输入参数大致可以分为以下几种(各种给出的具体方式可能不同):矩阵尺寸、类型、矩阵数据值、步长。
- 矩阵尺寸给出的是矩阵的大小(行列数),通常的方式有枚举形式([rows,cols])、向量形式(vector& size)、数组指针(int * size);
- 类型就是之前定义的Array Type;
- 矩阵数据值通常是一个指向矩阵的指针(int* data);
- 步长也通常是一个数组指针(size_t * step)

为什么Mat中的普通指针(uchar *data)能够被指向局部变量,但是我写一个类,指向局部变量,就被回收了呢?

posted @ 2020-05-04 12:38  0点0度  阅读(201)  评论(0编辑  收藏  举报