could u plz give 4 little files-sample (c and c++ code and header files) which shows how to call c++ class public member function.
thanx.
c++ classes and their methods are not callable from C.
You write a C callable wrapper that runs the method.
#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;
}
}
#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;
}
link doesn't work
But the link works perfectly for me.
But the link works perfectly for me.
It times out from here (USA).
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-lite/mixing-c-and-cpp.html
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.