using c++ objects from C

This is a discussion on using c++ objects from C within the C++ Programming forums, part of the General Programming Boards category; I'm trying to use c++ objects from my c code. I need to create the c++ object, and make multiple ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    5

    Thumbs down using c++ objects from C

    I'm trying to use c++ objects from my c code. I need to create the c++ object, and make multiple calls from the c code to the methods in the class.

    Here's what I've implemented:

    In the C file I created a structure to be the pointer to the c++ class:

    Code:
    /* create a pointer that will actually be a pointer to the c++ object */
    struct myClass* pMyStruct;
    In that same file, I made a call to a c function in the c++ file passing that pointer:
    Code:
    call_class_init(pMyStruct);
    In the cpp file is the c++ wrapper function as well as the myClass class. Is this right? Is this how the object is created and the reference to it is set?:

    Code:
    extern "C"  void call_class_init(myClass* p1)
     {
                     myClass fred;
                    p1= &fred;
                    p1->init_class_obj();
    }
    And then in the init_class_obj() method I do a bunch of class implementation stuff of the c++ class.

    So then in the C file I can make other calls to the c++ class by having more extern C methods to call using the same pointer:

    Code:
    set_count(pMyStruct, 4);
    and in the c++ file:
    Code:
    extern "C" void set_count(myClass* p1, int count)
    {
             p1->set_count(count);
    }
    Does this all make sense? Is there an easier way? With my implementation the 1st call works, and then when I make another call to the c++ object, it fails.

    any help, sample code, or links would be great. I did a search for "mix c c++" and found a few results via google.

    Thanks!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    5
    I found that example before.

    But my scenario is a little different. In that example, the c++ class creates and instance of Fred, and calls the c function passing the instance of the c++ object.

    I need the opposite with the c function to call the c++ class, and the it returns a reference to the c++ object.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    C doesn't do references, so it needs to be a pointer.
    Also, make sure your main() is in C++ land and not C land.

    Is there a problem with making it all C++?
    The 'C' part of a mixed program is usually the role of some pre-existing library.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    5
    Salem,

    Thanks for your reply. Sorry, when I said reference I meant pointer.
    The main is in c++. And I have to use C since it's pre-existing code that has to be re-used.

    So do I just create a function in the c++ file that returns a pointer to a new c++ object? But if I create that object in that function, it'll be destroyed once the function completes, right? And I don't want to use global objects.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    How about posting some code which illustrates what it is you're trying to do.
    A few simple 1-member objects and a few 5-line functions ought to be enough.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    5
    I suppose the code I posted at the start isn't much help. I've changed the code somewhat so the c++ main() does all the work, and passes the object pointers to the c code.

    here's the main:
    Code:
    int main(int argc, char* argv[])
    {
            TToiVideoMixerCaller* videoMixer;
    ......
    ......        	
      	videoMixer = new TToiVideoMixerCaller(&ipcClient, videoMixerAddress);
    	videoMixer->SetFullVideoWindow();               
    	video_driver_class kvd;
    	//set the videoMixer of the new object
    	kvd.set_video_mixer(videoMixer);
    	//call the init method
    	kvd.init_video_driver_obj();
    	//pass the pointer to the c function 
    	set_kvd_pointer(&kvd);
           //call the c code
    	the_main (argc, argv);
    }
    So the main creates a videoMixer object as well as a video_driver_class. It then passes a pointer of the videoMixer to the video_driver_class.

    Here's the c++ file:
    Code:
    	class video_driver_class
    	{
    	    TToiVideoMixerCaller* videoMixer;
    	  public:
    	  	virtual void init_video_driver_obj();
    	    virtual int get_colour_key();
    	    virtual void set_video_mixer(TToiVideoMixerCaller *pVmx);
    	    video_driver_class();
                video_driver_class(const video_driver_class&);
    	  private:
              protected:
    	};
        void video_driver_class::set_video_mixer(TToiVideoMixerCaller *pVmx)
        {
        	videoMixer = pVmx;
        }
        int video_driver_class::get_colour_key()
        {
        	fprintf(stderr, "entered get_colour_key\n");
        	int colourKey;
        	videoMixer->GetColorKey(colourKey);	
        	return colourKey;
        	fprintf(stderr, "leaving get_colour_key\n");
        }
        void video_driver_class::init_video_driver_obj() 
        {
            videoMixer->SetFullVideoWindow();
    	int colourKey;
    	videoMixer->GetColorKey(colourKey);
    	fprintf(stderr,"videoMixer colourKey = %d\n",colourKey);
    	fprintf(stderr, "leaving init_kreatel_video_driver_obj()\n");
      }
    
    extern "C" 
    {
    	int get_colourkey_kvd_video_window(video_driver_class* kvd)
    	{
    	 	return kvd->get_colour_key();
    	}
    }
    And here's the c file:
    Code:
    #include "video_driver_obj_mapping.h"
    /* create a pointer that will actually be a pointer to the c++ object */
         struct video_driver_class* pVideoStruct;
    
    void set_kvd_pointer(video_driver_class* kvd)
    {
    	pVideoStruct = kvd;
    }
    
    BOOLEAN
    driver_PlatformPorts_device_video_init ()
    {
        	fprintf(stderr, "video window colour key = %d\n" + get_colourkey_kvd_video_window(pVideoStruct));
        }
    
        return TRUE;
    }
    So, when the main() calls: kvd.init_video_driver_obj(), I see the colourkey. But then, when the c function calls: get_colourkey_kvd_video_window(pVideoStruct) The application dies.

    I'd appreciate any and all help. Thanks!

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    5
    looks like I found the problem. I guess I was coding with my Java hat on:
    fprintf(stderr, "video window colour key = %d\n" + get_colourkey_kvd_video_window(pVideoStruct));

    Duhh... notice the '+' symbol instead of the comma?

    Arrgg, I hate those ones that the compiler doesn't catch.

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    > Arrgg, I hate those ones that the compiler doesn't catch.
    If you're using gcc / g++, these options would have.
    -W -Wall -ansi -pedantic -O2
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. most efficient way to write filestream objects?
    By darsunt in forum C++ Programming
    Replies: 3
    Last Post: 01-26-2009, 04:17 PM
  2. Replies: 60
    Last Post: 12-20-2005, 10:36 PM
  3. Question about cout an stack object?
    By joenching in forum C++ Programming
    Replies: 8
    Last Post: 05-08-2005, 10:10 PM
  4. chain of objects within pop framework help needed
    By Davey in forum C++ Programming
    Replies: 0
    Last Post: 04-15-2004, 10:01 AM
  5. array of objects?
    By *~*~*~* in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2003, 05:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21