C++/JNI

P

Pradeep

Hi,

I have a DLL which is built in C++. This C++ DLL has a JNI Interface
and the methods are called from Java.

I want to know how to define the default / Optional parameters in C++
so that they can be called from Java.

Say for example I have a method add in C++

int add (int x = 5, int y = 10);

Can this method be called from Java through JNI by such a call

int z = object.add();

Or do i need to overload the add method in C++?

Another question I have is that can i use default values in a way such
that the next value has the optional value of the previous one
say

add( int x = 5, int y = x);

Or do i need to overload the method in this case also?

Thanks in Advance
Pradeep
 
S

Stefan Schulz

Hi,

I have a DLL which is built in C++. This C++ DLL has a JNI Interface
and the methods are called from Java.

I want to know how to define the default / Optional parameters in C++
so that they can be called from Java.

Say for example I have a method add in C++

int add (int x = 5, int y = 10);

Can this method be called from Java through JNI by such a call

int z = object.add();

Or do i need to overload the add method in C++?

Default parameters are "syntactic sugar" offered by C++ for overloading
methods. Java does not offer this dangerous bit of candy (and i sure hope
it never will). You will need to define java methods for each intentional
mode of invocation.
 
P

Pete Barrett

Default parameters are "syntactic sugar" offered by C++ for overloading
methods. Java does not offer this dangerous bit of candy (and i sure hope
it never will). You will need to define java methods for each intentional
mode of invocation.

Even in C++, I think you'll find that the substitution of the default
parameters occurs at compile time, rather than at run time, so the
actual call includes all the parameters. Since the Java program will
necessarily invoke the C++ methods at run time, the call from Java
will need to supply all the parameters.

Pete Barrett
 
R

Roedy Green

Even in C++, I think you'll find that the substitution of the default
parameters occurs at compile time, rather than at run time, so the
actual call includes all the parameters. Since the Java program will
necessarily invoke the C++ methods at run time, the call from Java
will need to supply all the parameters.

The equivalent feature in Java is the ability to define a method with
the same name but fewer parms. You then implement it with a call to
the many-parm method filling in defaults.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top