Click here to Skip to main content
15,886,840 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i found the code below for OpenCV 2.4.13

------------------------core.hpp---------------------------------
1920: uchar* ptr(int i0=0);
const uchar* ptr(int i0=0) const;

template<int n> uchar* ptr(const Vec<int, n>& idx);
template<int n> const uchar* ptr(const Vec<int, n>& idx) const;
------------------------------------------------------------------
there are two declaration for same function and template uchar* ptr(), with and without const;
how compiler will handle this? why need write these two version?

What I have tried:

how compiler will handle this? why need write these two version?
Posted
Updated 13-Dec-17 4:55am

1 solution

The compiler chooses the version that matches in the context:
C++
// The const version is used here
uchar c = *var.ptr();
const uchar *p = var.ptr();

// The non const version is used here
*var.ptr() = c;
uchar *p = var.ptr();

It is not really necessary to provide the const version but it is recommended, good style, and prevents from changing values inadvertently where this is checked at compile time:
C++
// The const version is used here
const uchar *p = var.ptr();
// Will throw a compiler error
*p = c;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900