![]() |
![]() |
![]() |
![]() |
![]() |
Example usage of some C/C++ functionality
Overloaded Operators
This example illustrates how to call an operator which has been made accessible by CPP2SDL. There are two operators defined and used, the first being a member operator, and the second a non-member operator. Also see Overloaded Operators.
class CInt {int val;public:CInt() : val(0) {};CInt(int i) : val(i) {};int value() const { return val; };int operator+(const int& i){val+= i; return val;};};int operator+(const int& left, const CInt& right) {return right.value()+left;};NEWTYPE global_namespace_ImpSpec /*#NOTYPE*/OPERATORS"+" /*#REFNAME 'operator+'*/ : int, CInt -> int;ENDNEWTYPE global_namespace_ImpSpec;EXTERNAL 'C++';NEWTYPE ptr_CInt Ref( CInt);OPERATORSptr_CInt : -> ptr_CInt;ptr_CInt : ptr_CInt -> ptr_CInt;ENDNEWTYPE ptr_CInt;EXTERNAL 'C++';NEWTYPE CIntOPERATORS"+" /*#REFNAME 'operator+'*/ : CInt, int -> int;CInt : -> CInt;CInt : int -> CInt;value : CInt -> int; /*#CONSTANT*/CInt : CInt -> CInt;ENDNEWTYPE CInt;EXTERNAL 'C++';String handling
It is possible to do C-style string handling in SDL, by using the standard C header string.h. By including `string.h' and `stdio.h' we are given access to the functions defined within them in SDL. You may notice that strcpy is defined in the hand written header as well as in `string.h'. The former definition allows us to assign the string "goodbye" to empty, without using the return value of strcpy, and importing it to SDL. Also needed is an allocate and deallocate function. An example allocate function has been defined in the header, a deallocate function should also be done in the practice to avoid memory leaks.
#include<string.h>#include<stdio.h>#ifdef __CPP2SDL__void strcpy(char*,char*);#endiftypedef char* string;char ara[10];string hello= "hello";string empty;char* allocateString(int length) {return (char*) calloc(length,sizeof(char));}NEWTYPE global_namespace_ImpSpec /*#NOTYPE*/OPERATORSmemcpy : ptr_void, ptr_void, size_t -> ptr_void;memcmp : ptr_void, ptr_void, size_t -> int;memset : ptr_void, int, size_t -> ptr_void;_strset : ptr_char, int -> ptr_char;strcpy : ptr_char, ptr_char -> ptr_char;strcat : ptr_char, ptr_char -> ptr_char;strcmp : ptr_char, ptr_char -> int;strlen : ptr_char -> size_t;....unlink : ptr_char -> int;strcpy : ptr_char, ptr_char;allocateString : int -> ptr_char;ENDNEWTYPE global_namespace_ImpSpec;EXTERNAL 'C';...SYNTYPE string = ptr_charENDSYNTYPE string;EXTERNAL 'C';/*#SDTREF(TEXT,header_CPP2SDL.i,728,6)*/NEWTYPE arr_10_char CArray( 10, char);ENDNEWTYPE arr_10_char;EXTERNAL 'C';/*#SDTREF(TEXT,header_CPP2SDL.i,728,6)*/DCL ara arr_10_char; EXTERNAL 'C';/*#SDTREF(TEXT,header_CPP2SDL.i,729,8)*/DCL hello string; EXTERNAL 'C';/*#SDTREF(TEXT,header_CPP2SDL.i,730,8)*/DCL empty string; EXTERNAL 'C';Type conversion
Some type conversions are easier in C/C++ than in SDL, in particular those that are implicit. An implicit type conversion must often be explicit in SDL, by introducing a simple cast operator that performs the conversion. For example, by using the mapping of SDL sorts between unsigned_char and char we can create a cast operator in SDL that converts a char to an Octet. (See Fundamental Types for more information). This corresponds to the implicit conversion in C/C++ between char and unsigned char.
http://www.ibm.com/rational |
![]() |
![]() |
![]() |
![]() |