Chapter Contents |
Previous |
Next |
Converting a C Program to a C++ Program |
Reserved Keywords |
The following keywords cannot be used as identifiers in C++ programs because they are reserved:
asm
|
new
|
this
|
catch
|
operator
|
throw
|
class
|
private
|
try
|
const_cast
|
protected
|
typeid
|
delete
|
public
|
typename
|
dynamic_cast
|
reinterpret_cast
|
virtual
|
friend
|
static_cast
|
|
inline
|
template
|
|
Function Prototypes |
int myfunc();
In C++, this same statement is interpreted as a prototype equivalent to the following:
int myfunc(void);
If you mistakenly
use the
myfunc()
form in your C++ program as a prototype and then call the
function with one or more parameters, you will receive error messages. In
converting a program from C to C++, you must change all of your nonprototypical
function declarations to prototypes.
"C" Linkage |
Multiple Declarations of File-Scope Variables |
In ANSI
C, a file-scope variable can be declared more than once without using the
extern
keyword. This is not legal in C++. To convert your program from C
to C++, make sure that all but one declaration of a file-scope variable has
the
extern
keyword. If the file-scope variable is initialized, the initializer
should be attached to the one declaration without the
extern
keyword.
Assigning Integers to enums |
In C++, an integer cannot be assigned directly to an
enum
as it could be in C. If
your program contains such assignments, first cast the integer to the
enum
type, as in the following example:
int i; enum X {a, b, c} e; e = i; /* Legal in C, not in C++. */ e = (enum X)i; // Legal in C++.
Of course, the best approach is to
use the correct
enum
constant in the first place.
Assigning void* Values to Pointers |
In C++, a
void*
pointer can be assigned only to another
void*
pointer. If your C program contains
assignments of
void*
pointers to other kinds of pointers, you should
cast the
void*
to the other
pointer type before the assignment, as in the following example:
int* ip; void* vp; ip = vp; /* Legal in C, not in C++. */ ip = (int*)vp; // Legal in C++.
Using Strings to Initialize a Character Array |
char a[3] = "SAS"; /* Legal in C, not in C++. */ // Legal in C++, keeps the terminating null // character. char a[4] = "SAS"; // Legal in C++, omits the null-character. char a[3] = {'S', 'A', 'S'};
Using File-Scope Constants |
In C, file-scope constants are external by default. In
C++, file-scope constants are
static
by default. You can take two
approaches to converting this type of problem:
extern
to the declaration of the file-scope constant that has the initialization.
extern
keywords
attached to declarations of the constant.
Using #pragma linkage |
The SAS/C extension
#pragma linkage
is supported only within
extern "C"
linkage blocks in C++ programs. Programs
that use the
#pragma linkage
feature outside
extern "C"
blocks should be changed
to use the
__ibmos
keyword.
To change uses of
#pragma linkage
, remove the
#pragma
linkage
and add
__ibmos
to the declaration of the function that needs
IBM OS linkage. The following example shows equivalent C and C++ usage:
/* Meaningful only in C. */ #pragma linkage (fname, OS) int fname(); // Meaningful in both C and C++. __ibmos int fname();
__ibmos
functions always have "C" linkage. For more
information on the
__ibmos
keyword, refer to the SAS/C Compiler
and Library User's Guide, Fourth Edition.
Identical Class Names and typedef Names |
In C++, if a
class name and a
typedef
name are the same, the
typedef
name
must refer to the class. If your C program contains a class (structure) name
and a
typedef
name that are the same, but refer to different things, you
must change one of the names. Here is an example:
/* This code is legal in C, but not in C++, because VAL refers to an int and a struct. */ typedef int VAL; struct VAL { long x; char *text; }; // This code is legal in both C and C++, // because VAL refers to the same thing in each // case. struct VAL { long x; char *text; }; typedef struct VAL VAL;
Embedded Structure Tags |
In C, a structure tag can be defined inside the definition
of some other structure tag. This is also legal in C++, but because structures
(classes) are scopes in C++, the inner structure tag is hidden inside the
outer structure. If you do not want this scoping effect, do not embed structure
tags. (In other words, move the inner structure's definition outside the outer
structure's definition, or use the scope operator (
::
) to use the inner structure's tag
in subsequent instantiations, as in
outer::inner
.)
Using goto Statements |
C++ does not allow you to use the
goto
statement
to jump over an initialization. If your C program contains such jumps, you
must remove them.
Differing Types |
C++
defines the types of character literals and enumerations differently than
C does.
In C, the type of a single-character
character literal (for example, '
H
') is
int
. In C++, character literals are
of type
char
. You do not usually have to worry about this distinction, unless
your program expects the result of
sizeof('H')
to be greater than one.
In C, the type of an
enum
is
int
. In C++, enumerations have type
enum
. Again, you do not
usually
have to worry about this distinction; just remember that the size of an
enum
may be different in C and in C++.
Chapter Contents |
Previous |
Next |
Top of Page |
Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.