[Search for users] [Overall Top Noters] [List of all Conferences] [Download this site]

Conference turris::c_plus_plus

Title:C++
Notice:Read 1.* and use keywords (e.g. SHOW KEY/FULL KIT_CXX_VAX_VMS)
Moderator:DECCXX::AMARTIN
Created:Fri Nov 06 1987
Last Modified:Thu Jun 05 1997
Last Successful Update:Fri Jun 06 1997
Number of topics:3604
Total number of notes:18242

3559.0. "CAlling class moethods from C??" by KERNEL::PULLEY (Come! while living waters flow) Wed Apr 30 1997 11:13

CAn anyone tell me why this works?

I'm using VMS v5.5-2, C++ v5.5.
(My customer's after something to use class methods in C++ called from C,
on UNIX).
I didn't expect this to work because I thought I'd only declared a pointer
to teh class whatever.
Have I really made an instance of the class objects as well?

Thanks & regards,
Steve.

C program to driver C++ function:-
--------------------------------------------------------------------------------
main()
{
	access_class();
}
--------------------------------------------------------------------------------

C++ small class & functions:-
--------------------------------------------------------------------------------
#include <iostream>
extern "C" int access_class();
class whatever {
public:
	void printit()
	{
		cout << "nothing much to say" << endl;
	}
};
whatever *s1;
void we_access(whatever *s1)
{
	s1->printit();
}
int access_class()
{
	we_access(s1);
	return 0;
}
--------------------------------------------------------------------------------
T.RTitleUserPersonal
Name
DateLines
3559.1mechanism explanation, but uselessCAIRN::HARRISKevin Harris, dtn 381-2039Wed Apr 30 1997 11:478
Steve,
	As I understand it, methods (i.e. member functions) of classes are
effectively static.  I.e. you have exactly one copy of each regardless of the
number of class instances that you declare, even 0.  This is the property that
allows you to call a member function even though you appear to have no variables
allocated of that class.  Of course, this property isn't very useful, since
there are no data members to read or write!
						-Kevin
3559.2KERNEL::PULLEYCome! while living waters flowThu May 01 1997 06:3412
    Ah thanks!
    
    So if I did have data members, am I going to be able to keep any class
    instances in scope anyway?
    I.e., I need C++ functions within which to declare the class, and
    they'll destruct again when they go out of scope, when I leave that
    function.
    Is that right?
    
    Cheers,
    Steve.
    
3559.3TUXEDO::WRAYJohn Wray, Distributed Processing EngineeringThu May 01 1997 10:1155
>    So if I did have data members, am I going to be able to keep any class
>    instances in scope anyway?
>    I.e., I need C++ functions within which to declare the class, and
>    they'll destruct again when they go out of scope, when I leave that
>    function.
>    Is that right?
    
    Not sure what you mean by the above.  When we've needed to do something
    like this, we've exported a "C image" of the object(s), and provided
    C-callable routines to invoke each member function that we want to
    expose, as shown below.  Data members are normally accessed only by the
    C++ code, but if you want to expose them you can add 'extern "C"'
    functions to read and write them.  The only thing to watch out for is
    that, if you want your code to be reasonably portable, you have to make
    sure that your C++ code doesn't have any static object declarations,
    because a C main program may not construct such objects on some
    platforms.
    
    John
    
    class CPP_CLASS {
    ...
    public:
    	function1(...);
        function2(...);
        ...
    	CPP_CLASS(params); // Constructor
    	~CPP_CLASS(); // Destructor
    };
    
    extern "C" void * create_object(params) {
       CPP_CLASS * object;
       object = new CPP_CLASS(params);
       return (void *)object;
    };
    
    extern "C" void destroy_object(void * Cobj) {
       CPP_CLASS * cpp_object;
       cpp_object = (CPP_CLASS *)Cobj;
       delete cpp_object;
    };
    
    extern "C" void funct1(void * Cobj, ...) {
       CPP_CLASS * cpp_object;
       cpp_object = (CPP_CLASS *)Cobj;
       cpp_object->function1(...);
    };
    
    extern "C" void funct2(void * Cobj, ...) {
       CPP_CLASS * cpp_object;
       cpp_object = (CPP_CLASS *)Cobj;
       cpp_object->function2(...);
    };