A similar situation arises when using host variables within C++ classes. It is frequently convenient to declare your class in a separate header file. This header file might contain, for example, the following declaration of my_class.
typedef short a_bool; #define TRUE ((a_bool)(1==1)) #define FALSE ((a_bool)(0==1)) public class { long host_member; my_class(); // Constructor ~my_class(); // Destructor a_bool FetchNextRow( void ); // Fetch the next row into host_member } my_class; |
In this example, each method is implemented in an embedded SQL source file. Only simple variables can be used as host variables. The technique introduced in the preceding section can be used to access a data member of a class.
EXEC SQL INCLUDE SQLCA; #include "my_class.hpp" #if 0 // Because it ignores #if preprocessing directives, // SQLPP reads the following declaration. EXEC SQL BEGIN DECLARE SECTION; long this_host_member; EXEC SQL END DECLARE SECTION; #endif // Macro used by the C++ compiler only. #define this_host_member this->host_member my_class::my_class() { EXEC SQL DECLARE my_table_cursor CURSOR FOR SELECT int_col FROM my_table order by int_col; EXEC SQL OPEN my_table_cursor; } my_class::~my_class() { EXEC SQL CLOSE my_table_cursor; } a_bool my_class::FetchNextRow( void ) { // :this_host_member references this->host_member EXEC SQL FETCH NEXT AllRows INTO :this_host_member; return( SQLCODE != SQLE_NOTFOUND ); } void main( void ) { db_init( &sqlca ); EXEC SQL CONNECT "DBA" IDENTIFIED BY "SQL"; { my_class mc; // Created after connecting. while( mc.FetchNextRow() ) { printf( "%ld\n", mc.host_member ); } } EXEC SQL DISCONNECT; db_fini( &sqlca ); } |
The above example declares this_host_member
for the SQL preprocessor, but the macro causes C++ to convert it to this->host_member
. The preprocessor would otherwise not know the type of this variable. Many C/C++ compilers do not tolerate duplicate declarations.
The #if
directive hides the second declaration from the compiler, but leaves it visible to the SQL preprocessor.
While multiple declarations can be useful, you must ensure that each declaration assigns the same variable name to the same type. The preprocessor assumes that each host variable is globally known following its declaration because it can not fully parse the C language.
Discuss this page in DocCommentXchange.
|
Copyright © 2010, iAnywhere Solutions, Inc. - SQL Anywhere 12.0.0 |