Tell me more ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I want to create a function in a C++ class or in the files of the class, but I want it to take two objects as arguments, shush like:

    float function (Object a, Object b){
        return float;
    };

But I don't want to write in the main program:

    Object a;
    Object b;
    float c;
    c = a.function (a, b);

I want to main program to look like this:

    Object a;
    Object b;
    float c;
    c = function (a, b);

So, where do I have to define this function?

share|improve this question
6  
This should be asked on Stack Overflow stackoverflow.com – Adam Thompson Oct 22 '11 at 15:51

closed as off topic by enzotib, hhlp, htorque, Nathan Osman, Bruno Pereira Oct 22 '11 at 16:56

Questions on Ask Ubuntu are expected to relate to Ubuntu within the scope defined in the FAQ. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about closed questions here.

1 Answer

C++ is just C with classes (and other stuff). You can define a function like in C in any .cc file

float youFunctionName(Object a, Object b){

    float returnNumber;

    //do whatever you want...

    return returnNumber;

}

and call from any other function or method as normal function:

c = youFunctionName(a,b);
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.