1

k this is kind of noob of me since I really haven't worked on C and objective c all that much.

Mat mymat(myIplImage, true);

This apparently not only declares mymat as a local variable but also copies the contents over from myIplImage. The problems I can't make of the syntax. I would be more comfortable if it were something like this:

Mat mymat = new Mat(myIplImage, true); // in c++

Can you explain what happened in the background for the original statement?

Thanks,

Simon
  • 31,675
  • 9
  • 80
  • 92
Haoest
  • 13,610
  • 29
  • 89
  • 105

2 Answers2

2

This is C++ actually. The first statement does basically the same thing as the second statement except that you don't handle the memory yourself, that is you don't have to delete mymat when you don't need it anymore, it will be destructed automatically when it goes out of scope.

Simon
  • 31,675
  • 9
  • 80
  • 92
  • Is this is some kind of operator overload effect? – Haoest Mar 13 '11 at 04:18
  • 2
    Not really, this is just calling the constructor. It is a very common way to instantiate an object in C++. – Simon Mar 13 '11 at 04:21
  • @Haoest: No, it has nothing to do with operator overloading. It declares an *automatic variable* in which its destructor is automatically called at the end of its scope. If you would like to learn more, I recommend that you pick up [a good C++ book recommended by the Stack Overflow C++ community](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). It'll talk about this and more. – In silico Mar 13 '11 at 04:42
  • I see. Is this a new feature in c++? I haven't code in it for like 5 years. – Haoest Mar 13 '11 at 07:40
  • @Haoest: It's not exactly new - it's been that way since the first C++ release in 1985. – molbdnilo Mar 13 '11 at 08:16
2

As stated by Simon, the first version handles the memory allocation for you. In the first version, behind the scenes, there's probably a new call or malloc or something similar in the constructor, and a delete or free call in the destructor for Mat. That way, if you're just making one Mat (as opposed to an array of them), you don't need to think much about the allocation.

Keep in mind you can always look at the source for the Mat constructor or any other portions of OpenCV if you ever want to get all the nitty gritty details.

SSilk
  • 2,433
  • 7
  • 29
  • 44