Home > Net >  Understand C const functions
Understand C const functions

Time:08-20

I am new to c and im trying to write a simple c wrappers to integrate with this third party c library; which comes with bad documentation.

function to integrate with (this is all the documentation that came with this function):

 virtual ImageCoord Raster::groundToImage(const XyzCoord & groundPt, 
              double desiredPrecision=0.001, 
              double* achievedPrecision=null) const

Text about function: This method converts the given groundPt (x,y,z in meters) to a returned image coordinate (line, sample in full image space pixels).

there is also some class documentation

Raster Class Ref
inherits from GeoModel
Public member functions:

virtual ImageCoord  groundToImage (const XyzCoord &groundPt, double desiredPrecision=0.001, double *achievedPrecision=NULL) const =0

in my code i have:

   //this is implemented correctly 
    const XyzCoord xyz(284971.17549099098, -126866.36533847413, 6350003.627515804)
    double desiredPrecision = 0.000001;
    double achievedPrecision = 0.0;

    // not sure if this is what the documentation meant by "image coordinate" but it comes with the library
    // ImageCoord(double line, double point)
    ImageCoord imagePoints;

    // the part im confused about, what is the proper way to invoke the above function, the below line was me trying out how to call the method 
    const Raster::groundToImage(&ecef, imagePoints);

Sorry for my ignorance in c but I've been baffled. I have lot of programing experience (8 plus years, just none with c so i understand programing terms, concepts and design patterns). Im also trying to understand in the function defination what does this mean

const XyzCoord & groundPt

CodePudding user response:

This is a non-static member function of a class named Raster.

You are supposed to invoke it via member access obj.groundToImage(/*args*/) where obj is an object of the class type to which the function belongs or a class type derived from that class.

Or, if the call happens inside another non-static member function of the same class or a derived class, it could just be groundToImage(/*args*/) which will call the function implicitly on the current object.

With virtual it may also be possible to invoke the function on an object of a base class of Raster, depending on where the virtual function has been declared first in the class hierarchy.

There are certain more specialized situations where a qualified call replacing groundToImage with Raster::groundToImage in either of the above could also be the intended behavior.

The const qualification of the function is irrelevant. It just means that the function can be called whether obj is const-qualified or not. Similarly the const-qualification on the function parameter is irrelevant. It just means that you can pass either a const- or non-const-qualified first argument. You don't have to make xyz const to be able to pass it. Only the other way around, passing a const-qualified expression to a non-const reference parameter is a problem. const is intended to signal that the function will not modify the argument and therefore doesn't care whether or not it is const-qualified.

const only makes sense when used in a declaration or type. Something like

const Raster::groundToImage(&ecef, imagePoints)

as a supposed function call doesn't make syntactical sense.

The function also expects up to three arguments of the specified types and returns a ImageCoord. You are not supposed to pass one as an argument. It is what the function returns.

The arguments should probably be xyz, desiredPrecision and &achievedPrecision given that you already declared them with the correct types.

It probably wants the last one as pointer because it is an out-parameter.

What the object on which the member function is called on is supposed to be is unclear from what you have shown. We don't know what ecef is though...

CodePudding user response:

TL;DR:

ImageCoord imagePoint = someRasterObject.groundToImage(
    xyz,
    desiredPrecision,
    &achivedPrecision
);

Or

ImageCoord imagePoint = somePointerToRasterObject->groundToImage(
    xyz,
    desiredPrecision,
    &achivedPrecision
);

From the signature given:

virtual                                 // A derived class's implementation of 
                                        //   this function can be called via a
                                        //   pointer or reference to a parent
                                        //   class object
ImageCoord                              // This function returns an ImageCoord object
Raster::                                // This is a member of the Raster class
groundToImage(                          // This function is named groundToImage
     const XyzCoord & groundPt,         // The first argument to this function is a
                                        //   reference to a constant XyzCoord object
     double desiredPrecision=0.001,     // The second argument is a double with a default
                                        //   value of 0.001 if not provided
     double* achievedPrecision=null     // The third argument is a pointer to a double
                                        //   with a default value of null if not provided
)
const                                   // This function can be called on a constant Raster
                                        //   object

That means you need 2-4 things to call this function:

  1. A (possibly const-qualified) Raster object to call the function on
  2. An XyzCoord object to pass as the first parameter
  3. (Optional) A double to pass as the second parameter
  4. (Optional) A pointer to a double to pass as the third parameter

While nothing in your question explicitly states it, I would assume the function uses the 3rd parameter as an output. I would assume it writes the actually achieved precision to the double pointed to by the pointer you pass it, so you'll probably want to just pass it the address of a local double variable.

CodePudding user response:

Each non-static method in a class is called on behalf of some object of that class (or some derived class), and the object is accessible within a method by an implicitly defined this pointer.

The const qualifier appended after the parameters' list of the method applies to that this value. In other words, it declares this of a type classname const* instead of classname*.

As a result the compiler will reject any attempts to modify the *this object from within the const-qualified method, so the method can be safely used on non-modifiable objects.

For example, the length() method of the std::string class is declared as

 size_t length() const;

so when you use it like, say:

std:string s;
....
size_t len = s.length();

you can be sure the s variable will not be modified during calculation of len value.

  •  Tags:  
  • c
  • Related