| | |
calling oo class member function from c
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
link doesn't work
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
You write a C callable wrapper that runs the method.
C Syntax (Toggle Plain Text)
#include <cstring> #include <sstream> #include <string> #include <vector> using namespace std; class Split { vector<string> result; public: Split( const string& s ) { stringstream sin( s ); string temp; while ( sin >> temp ) { result.push_back( temp ); } } vector<string> Result() const { return result; } }; extern "C" { char **SplitString( const char *s ) { vector<string> temp = Split( s ).Result(); char **result = new char*[temp.size() + 1]; // Terminate the result array with a sentinel. result[temp.size()] = 0; for ( vector<string>::size_type i = 0; i < temp.size(); ++i ) { // Add a new C-string to the result array result[i] = new char[temp[i].length() + 1]; // Initialize the new string from the temp result strcpy( result[i], temp[i].c_str() ); } return result; } }
c Syntax (Toggle Plain Text)
#include <stdio.h> extern char **SplitString( const char *s ); int main( void ) { char **p = SplitString( "this is a test" ); while ( *p ) { printf( "%s\n", *p++ ); } return 0; }
The truth does not change according to our ability to stomach it.
If there is any C++ in your program, then the whole thing is a C++ program, which just happens to use some C APIs
http://www.parashift.com/c++-faq-lit...c-and-cpp.html
http://www.parashift.com/c++-faq-lit...c-and-cpp.html
![]() |
Similar Threads
- performance benefit by not calling static member function by object (C)
- Abstract Class member function problem (C++)
- Visual Studio 6: Initializing static class member (C++)
- Practical application of static member function (C++)
- Is ifs is a member function of ifstream class (C++)
Other Threads in the C Forum
- Previous Thread: need help
- Next Thread: How do I eliminate some characters from the end of a string?
Views: 1252 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for C
* adobe api append array arrays bash binarysearch centimeter char character cm copyanyfile copypdffile createcopyoffile createprocess() csyntax directory drawing dynamic executable execv feet fgets file floatingpointvalidation fork frequency function getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux highest homework i/o ide infiniteloop initialization interest intmain() kilometer lazy license linked linkedlist linux linuxsegmentationfault list match matrix meter microsoft mqqueue multi mysql oddnumber odf open openwebfoundation pause pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition scheduling segmentationfault send shape single socketprogramming spoonfeeding stack standard strchr string strings student suggestions system test testautomation unix urboc user whythiscodecausesegmentationfault win32 win32api windows.h






