3

Long story short, I am trying to build a wrapper to access C++ source code from a C main function (I have to do the conversion because of Embedded systems); however, I am having trouble calling the methods from the class to an external function without creating an instance of that class.

I want to pass this *side pointer from my C code, calculate the cube of it, and get returned the cubed value. I have tested my wrapper with simple pointer functions and variables and it works perfectly fine, however I am having trouble with class methods. Here is my source code to that, with the mistake I am making on the last line...:

class Cube
{
public:
    static int getVolume(int *side)
    {
        return *side * *side * *side;     //returns volume of cube
    }
};

void Cube_C(int *side) 
{
    return Cube.getVolume(*side);
}
3
  • 1
    Why is side an int* instead of just an int?
    – Ted Lyngmo
    Commented Sep 21, 2020 at 15:11
  • Hello! It is an int* because I am inputting a the value from C. My C code looks like this: int cubeSide = 5; printf("Cube of 5 is: %d\n\n", GetVolume_C(&cubeSide)); In the meantime I use extern"C" on a header file to link the C with C++.
    – Polar Bear
    Commented Sep 22, 2020 at 11:42
  • 1
    I still don't see why you need to send it as a pointer instead of a plain int? If you check my example in the answer I gave, there are no pointers.
    – Ted Lyngmo
    Commented Sep 22, 2020 at 11:55

2 Answers 2

4

You can call a static member function of a class without an instance: just add the class name followed by the scope resolution operator (::) before the member function's name (rather than the class member operator, ., as you have tried).

Also, in your Cube_C function, you should not dereference the side pointer, as the getVolume function takes a int * pointer as its argument. And you need to declare the return type of that function as an int (not void):

int Cube_C(int *side) 
{
    return Cube::getVolume(side);
}
2
  • Thanks for your answer, I already tried that soultion however I still get an error of my return value not matching the function type .
    – Polar Bear
    Commented Sep 21, 2020 at 14:59
  • @PolarBear Oops - my mistake (see edited answer). I forgot the second correction to your Cube_C function! Commented Sep 21, 2020 at 15:01
2

For this particular example, you don't need them to be classes at all since Cube doesn't hold a state. Just make a function:

int Cube_Volume(int side) { return side * side * side; }

If you want objects that holds a state to be reusable from C, you do need an instance:

class Cube {
public:
    Cube(int side) : m_side(side) {}
    int getVolume() const { return m_side * m_side * m_side; }

private:
    int m_side;
};

Then in your C interface:

extern "C" int Cube_Volume(int side) { return Cube(side).getVolume(); }

Edit: I added a more verbose example at github to show how you can create C functions to create and manipulate C++ objects.

5
  • Wow thanks for this answer. I really need to study more of C++ to understand what is happening here but could be very useful for my future! (sorry that I cannot upvote... to new in stack overflow)
    – Polar Bear
    Commented Sep 21, 2020 at 15:07
  • 1
    @PolarBear That's ok :-) What happens in the part with the C++ class is that Cube(side) instantiates a temporary object with the side set to whatever the user calls Cube_Volume with. It then calls getVolume() on that temporary object. The object is then automatically destroyed. When optimized, this will likely just be doing side * side * side directly.
    – Ted Lyngmo
    Commented Sep 21, 2020 at 15:09
  • 1
    @PolarBear I started a github project (see update in the answer) that I'll probably expand on in the future. It contains just one example of a C++ class and C functions to create an object, call the object's member functions and then destroy it.
    – Ted Lyngmo
    Commented Sep 22, 2020 at 13:56
  • Thanks a lot on that matter. I will keep posting questions regarding that topic. Next useful expansion suggestions in case you will continue expanding would be template classes and dynamic (heap) memory allocation in a dynamic array from C++ to C.
    – Polar Bear
    Commented Sep 25, 2020 at 10:12
  • @PolarBear You're welcome! If I remember correctly, I've made a class-like set of C functions for dynamic memory management in an answer here, but that was in pure C. For C++ one could simply create a wrapper for std::vector<> for the specific types one wants to store. Since templates doesn't exist in C one would have to make an explicit instantiation for each type.
    – Ted Lyngmo
    Commented Sep 25, 2020 at 10:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.