Question

I need to delegate 'ceil' function. My class has method 'ceil', which need to return cpp's native method 'ceil'. How to call it?

double ceil() {
return ceil();
}

- this is a recursion

Was it helpful?

Solution

double ceil() {
    return ::ceil(something); // ceil actually has an argument
}

Of course, that above is when you define the method inside the class definition; the following is for when you define the method outside the class:

double MyClass::ceil() {
    return ::ceil(something);
}

And as the comment suggests, using std::ceil from included <cmath> is better, because indeed, ::ceil is not guaranteed to be the ceil from the C library.

OTHER TIPS

You can specify that you mean std::ceil by writing std::ceil.

That assumes you're including the C++ header, <cmath> not the deprecated C header <math.h>. In that case, and if you don't feel like including the correct header, it's in the global namespace and accessible as ::ceil.

Remember that it takes an argument, so the code you've posted won't work even after qualifying it.

Use std::ceil inside your customized ceil.

Better would be Have a cast operator to double, as

operator double() within your class to convert your data to double and call ceil(obj) wherever needed, so, you don;t have ceil as member within your class, and your object gets converted to double (through your cast operator) and std::ceil gets called automatically.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top