944,116 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 1436
  • C RSS
Sep 4th, 2007
0

calling oo class member function from c

Expand Post »
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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
asilter is offline Offline
60 posts
since Oct 2006
Sep 4th, 2007
0

Re: calling oo class member function from c

Have a look at this.
Reputation Points: 85
Solved Threads: 42
Nearly a Posting Virtuoso
vishesh is offline Offline
1,362 posts
since Oct 2006
Sep 4th, 2007
0

Re: calling oo class member function from c

c++ classes and their methods are not callable from C.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is online now Online
21,961 posts
since Aug 2005
Sep 4th, 2007
0

Re: calling oo class member function from c

Click to Expand / Collapse  Quote originally posted by vishesh ...
Have a look at this.
link doesn't work
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is online now Online
21,961 posts
since Aug 2005
Sep 4th, 2007
0

Re: calling oo class member function from c

You write a C callable wrapper that runs the method.
  1. #include <cstring>
  2. #include <sstream>
  3. #include <string>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. class Split {
  9. vector<string> result;
  10. public:
  11. Split( const string& s ) {
  12. stringstream sin( s );
  13. string temp;
  14.  
  15. while ( sin >> temp ) {
  16. result.push_back( temp );
  17. }
  18. }
  19.  
  20. vector<string> Result() const { return result; }
  21. };
  22.  
  23. extern "C" {
  24. char **SplitString( const char *s ) {
  25. vector<string> temp = Split( s ).Result();
  26. char **result = new char*[temp.size() + 1];
  27.  
  28. // Terminate the result array with a sentinel.
  29. result[temp.size()] = 0;
  30.  
  31. for ( vector<string>::size_type i = 0; i < temp.size(); ++i ) {
  32. // Add a new C-string to the result array
  33. result[i] = new char[temp[i].length() + 1];
  34.  
  35. // Initialize the new string from the temp result
  36. strcpy( result[i], temp[i].c_str() );
  37. }
  38.  
  39. return result;
  40. }
  41. }
  1. #include <stdio.h>
  2.  
  3. extern char **SplitString( const char *s );
  4.  
  5. int main( void ) {
  6. char **p = SplitString( "this is a test" );
  7.  
  8. while ( *p ) {
  9. printf( "%s\n", *p++ );
  10. }
  11.  
  12. return 0;
  13. }
Reputation Points: 180
Solved Threads: 34
Posting Whiz
Hamrick is offline Offline
322 posts
since Jun 2007
Sep 4th, 2007
0

Re: calling oo class member function from c

link doesn't work
But the link works perfectly for me.
Reputation Points: 85
Solved Threads: 42
Nearly a Posting Virtuoso
vishesh is offline Offline
1,362 posts
since Oct 2006
Sep 4th, 2007
0

Re: calling oo class member function from c

Click to Expand / Collapse  Quote originally posted by vishesh ...
But the link works perfectly for me.
It times out from here (USA).
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is online now Online
21,961 posts
since Aug 2005
Sep 4th, 2007
0

Re: calling oo class member function from c

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
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Fonts in VS 2005
Next Thread in C Forum Timeline: How do I eliminate some characters from the end of a string?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC