保持C ++对象,利用其方法取决于用户界面 [英] Maintain C++ Object, using its method depending on UI
问题描述
我有必须解决的一个问题:
I have an issue that need to be resolve:
我对此创建一个对象,并使用该对象的方法,本机方法在Java。
这是我的java code:
我有一个名为IssmJni一个java文件,它包含一个本地方法:
I have a java with native method that create an object and utilize method of that object. this is my java code: I have a java file called IssmJni and it contain a native method:
public static native long fac(long n);
static {
System.loadLibrary("FacLib");
}
public static long facIterative(long n)
{
return fac(n);
在我的主类我有这样的:
in my main class I have this:
long result = IssmJni.facIterative(Long.parseLong(input));
System.out.println(result);
这是我的C ++ code:
主文件:
this is my C++ code: main file:
static jlong factorial(JNIEnv *env, jclass clazz, jlong n)
{
fac *f = new fac();
jlong result = (jlong) (f->factorial(n));
delete(f);
return (jlong) result;
}
头文件:
class fac
{
public:
long factorial(long n);
};
fac.cpp:
fac.cpp:
#include "com_lan_factorial_FacLib.h"
#include "fac.h"
long fac::factorial(long n)
{
long f = 1;
long i;
for(i = 1; i <= n; i++)
{
f *= i;
}
return f;
}
一切工作正常,那么什么发生的是,用户界面将养活一个号码,称为C ++ code和生成一个新的对象调用FAC,并在该类FAC使用的方法阶乘。
该行
Everything works fine, so what happen is that the UI will feed a number, called the C++ code and generate a new object call fac, and use the method factorial in that class fac. by this line
FAC * F =新的FAC();
jlong结果=(jlong)(F->阶乘(N));
fac *f = new fac(); jlong result = (jlong) (f->factorial(n));
我要的是保持同一个对象FAC,但利用与不同的输入因法。我尽量远离远离pthread的,因为它太复杂了。有没有办法,我可以创建另一个Java线程,并保持这个对象,活在我的应用程序的全过程,并使用阶乘每当我有新的用户输入的方式。然后摆脱这个对象时,应用程序死了?
What I want is to keep the same object fac, but utilize factorial method with the different input. I try to stay away from pthread because it is too complicated. Is there a way that I can create another java thread and keep this object alive throughout the entire process of my app, and use that factorial whenever I have new user input. then get rid of this object when the app die?
总之,我想保持对象FAC的1个实例,并用阶乘方法很多次。是否有可能没有做本地的pthread?
谢谢
In sum, I want to keep 1 instance of object fac, and use factorial method many times. Is it possible without doing native pthread? Thanks
推荐答案
这是我想出。
static fac* pfac = NULL;
namespace com_lan_factorial
{
static jlong factorial(JNIEnv *env, jclass clazz, jlong n)
{
pfac = new fac();
jlong result = (jlong) (pfac->factorial(n));
return (jlong) result;
}
static jlong factorial_ten(JNIEnv *env, jclass clazz)
{
jlong result = (jlong) (pfac->factorial(3));
return (jlong) result;
}
所以我创建了一个名为PFAC指针,在第一次运行财务和行政委员会将被分配到一个新的对象PFAC =新FAC();在第二轮我只是重复使用的财务和行政委员会的指针和召回阶乘函数。这全部工作,但我不知道这是否在此创建2 FAC对象有2析法,或者只是1 FAC方法和相同的阶乘方法。任何人都可以验证,请。谢谢
so i create a pointer called pfac, upon the first run pfac will be assign to a new object pfac = new fac(); upon the second run I just reused the pfac pointer and recall the factorial function. This completely works, But I am not sure if this create 2 fac objects here with 2 factorial method, or just 1 fac method and the same factorial method. can anyone verify please. thanks
这篇关于保持C ++对象,利用其方法取决于用户界面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!