access the value of array by pointer in CLR

Kuo

I have a problem regarding accessing the value of array by pointer. In native C++ language, I can access the element by the following code

int a[]={1,2,3};
cout<<*(a+1);

So, I can get "2".

However, when I use C++ CLR and openCV 2.4.7, there is an error. The code as bellow.

cv::Mat a;
cout<<*(a+1);

I don't know how to deal with it after searching some websites. Wish anyone can help me. Thank you!

SHR

when you declare int a[] you should ask yourself what is a?

the answer is: a is array of integers. it is also the address of the first integer in the array.

so when you call *(a+1);

it been interpreted as:

  1. take the address of a and add size of int to it (a+1).

  2. go to the result address and take its content.

when you declare cv::Mat a; it is not an array. a is an object, not an address.

now (a+1) has any meaning only when cv::Mat has the operator +. otherwise it will result an error. even if it has the operator + I'll guess the return value will be a cv::Mat not an integer.

*(a+1) has no meaning since the result is not an address.

And finally ,to print it with a cout it also has to implement the stream operator operator<< with argument of cv::Mat.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related