calling oo class member function from c

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2006
Posts: 60
Reputation: asilter is an unknown quantity at this point 
Solved Threads: 0
asilter asilter is offline Offline
Junior Poster in Training

calling oo class member function from c

 
0
  #1
Sep 4th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1,311
Reputation: vishesh is on a distinguished road 
Solved Threads: 36
vishesh's Avatar
vishesh vishesh is offline Offline
Nearly a Posting Virtuoso

Re: calling oo class member function from c

 
0
  #2
Sep 4th, 2007
Have a look at this.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,651
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1498
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: calling oo class member function from c

 
0
  #3
Sep 4th, 2007
c++ classes and their methods are not callable from C.
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,651
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1498
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: calling oo class member function from c

 
0
  #4
Sep 4th, 2007
Originally Posted by vishesh View Post
Have a look at this.
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 322
Reputation: Hamrick will become famous soon enough Hamrick will become famous soon enough 
Solved Threads: 33
Hamrick's Avatar
Hamrick Hamrick is offline Offline
Posting Whiz

Re: calling oo class member function from c

 
0
  #5
Sep 4th, 2007
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. }
The truth does not change according to our ability to stomach it.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1,311
Reputation: vishesh is on a distinguished road 
Solved Threads: 36
vishesh's Avatar
vishesh vishesh is offline Offline
Nearly a Posting Virtuoso

Re: calling oo class member function from c

 
0
  #6
Sep 4th, 2007
Originally Posted by Ancient Dragon View Post
link doesn't work
But the link works perfectly for me.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,651
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1498
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: calling oo class member function from c

 
0
  #7
Sep 4th, 2007
Originally Posted by vishesh View Post
But the link works perfectly for me.
It times out from here (USA).
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: calling oo class member function from c

 
0
  #8
Sep 4th, 2007
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 1252 | Replies: 7
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC