#include <maindoc.h>
Public Member Functions | |
template<typename T> | |
void | operator() (const T &x) throw (TypeMismatch, UnknownMember) |
forwards a function call to the object it represents. | |
template<typename T> | |
T | operator= (T x) throw (TypeMismatch, UnknownMember) |
forwards an assignment to the object it represents. | |
operator int () const throw (TypeMismatch, UnknownMember) | |
access the data member as an int. | |
operator char () const throw (TypeMismatch, UnknownMember) | |
access the data member as a char. | |
operator long () const throw (TypeMismatch, UnknownMember) | |
access the data member as a long. | |
operator bool () const throw (TypeMismatch, UnknownMember) | |
access the data member as a bool. | |
operator double () const throw (TypeMismatch, UnknownMember) | |
access the data member as a double. | |
operator float () const throw (TypeMismatch, UnknownMember) | |
access the data member as a float. | |
operator const char * () const throw (TypeMismatch, UnknownMember) | |
access the data member as a const char *. | |
operator char * () const throw (TypeMismatch, UnknownMember) | |
access the data member as a char *. | |
Friends | |
class | Object |
This is a utility class that provides convenient access to the members and member functions of a dynamically-loaded class. Member objects are implicitly used, most often as temporaries, through the member method of the Object class. For example:
// // once we have an Object object // Class * employee_class = load_class("Employee", "employee.so"); Object * joe = employee_class.new_object("Joe"); // // we can access its data members and call its member functions // with a convenient syntax. // joe->member("salary") = 120; joe->member("add_project")("Testing SDL");
In addition to the assignment and the function call operators, this class implements a number of type-conversion operators, which are also useful to access data members. For example:
// // once we have an Object object // Class * employee_class = load_class("Employee", "employee.so"); Object * joe = employee_class.new_object("Joe"); // // we can access its data members and call its member functions // with a convenient syntax. // joe->member("salary") = 120; joe->member("add_project")("Testing SDL");
Notice that, while this syntax hides the type specification, a static type specification is still necessary to access member functions and data members. In fact, the type used is that of the right-hand side or that of the parameter, in the assignment and function call, respecitvely.
This class provides access to the values of data members presenting the same simple syntax. So, a (data) member can also be used as a value, for example, as the right-hand-side of an expression or as a parameter in a function call. However, the type of the expression must be either deduced by the context, as in a function call, or explicitly defined through a type cast. For example, given the following declaration:
void function_that_takes_an_int(int x);
we can write something like this:
function_that_takes_an_int(joe->member("salary"));
but we can't say something like this
cout << "Joe's salary is " << joe->member("salary") << endl;
because it is ambiguous. Instead we should explicitly type-cast the "method" expression:
cout << "Joe's salary is " << (int)joe->member("salary") << endl;