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.

Re: calling oo class member function from c 80 80
Member Avatar

Have a look at this.

Re: calling oo class member function from c 80 80

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

Re: calling oo class member function from c 80 80

Have a look at this.

link doesn't work

Re: calling oo class member function from c 80 80

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;
}
Re: calling oo class member function from c 80 80
Member Avatar

link doesn't work

But the link works perfectly for me.

Re: calling oo class member function from c 80 80

But the link works perfectly for me.

It times out from here (USA).

Re: calling oo class member function from c 80 80

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

Be a part of the DaniWeb community

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