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.

Member Avatar for GreenDay2001

Have a look at this.

c++ classes and their methods are not callable from C.

Have a look at this.

link doesn't work

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;
}
Member Avatar for GreenDay2001

link doesn't work

But the link works perfectly for me.

But the link works perfectly for me.

It times out from here (USA).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.