在 C++ 类的内部使用主机变量时会出现类似情况。在单独的头文件中声明类通常比较方便。例如,此头文件可能包含以下 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; |
在此示例中,每个方法都是在嵌入式 SQL 源文件中实现的。只有简单的变量才可用作主机变量。前一节介绍的技术可用于访问类的数据成员。
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 ); } |
以上示例为 SQL 预处理器声明 this_host_member
,但宏使 C++ 将其转换为 this->host_member
。预处理器并不知道该变量的类型。许多 C/C++ 编译器不允许重复的声明。#if
指令使编译器看不到第二个声明,但 SQL 预处理器可以看到它。
尽管多个声明可能十分有用,但必须确保每个声明将相同的变量名指派给相同的类型。由于预处理器不能完全分析 C 语言,因此它假定每个主机变量在声明后均为全局已知。
![]() |
使用DocCommentXchange讨论此页。
|
版权 © 2013, SAP 股份公司或其关联公司. - SAP Sybase SQL Anywhere 16.0 |