I have a very simple question.
I want to write the开发者_如何学运维 below line of code in 2 lines :
IplImage *image = 0;
like :
IplImage *image;
image = 0;
I want to know what I have written is correct or else I want to know how to write the correct one (in two lines).
Thanks
Perfectly correct. But if you don't have a very good reason to do it that way, I'd suggest leaving it as a single line that both declares and initializes it. It's more obvious, and you're less likely to ever miss initializing a pointer that way.
It is correct. Why didn't you just try it?
Writing
IplImage *image = 0;
seem to be clearer as it is obvious that a pointer is used.
With
IplImage *image;
image = 0;
you may have additional lines of code between the first and the second line. The second line (image = 0
) appears less clear to me. Maybe renaming the variable to pImage
improves readability if you prefer the second option (two liner).
Iplimage is the structure defined in opencv. You can use code below to initialize the pointer:
Iplimage* image=cvCreateImage(width,height,channels);
In C++
IplImage *image(NULL);
woul also be allowed, even if it may be confusing about your image to be allocated by calling the constructor with the first argument set to NULL.
However, declaring + initializing in 1 line is still remended and almost all case. The only case I see where it's better to initilize in 2 lines it's when the line is too long.
精彩评论