consider a simple example class:
class BankAccount { public: BankAccount() { balance =0.0; }; ~BankAccount() {}; void deposit(double amount) { balance += amount; } private: double balance; };
Now say that I want to wrap this in extern "C" so that I can call it from different programming languages ββsuch as C # and Java. I tried the following, which seemed to work:
// cbankAccount.h: extern "C" unsigned long createBackAccount(); extern "C" void deposit(unsigned long bankAccount, double amount); // cbankAccount.cpp unsigned long createBackAccount() { BankAccount *b = new BankAccount(); return (unsigned long) b; } void deposit(unsigned long bankAccount, double amount) { BankAccount *b = (BankAccount*) bankAccount; b->deposit(amount); }
Is it portable? Is the type unsigned long unsigned long large enough for an object pointer? Any other problems with this approach?
Thanks in advance for any answers!
Andreas WP
source share