Cs251-week5 - myswap.h#define mySwap mytype x y cfw the continues the definition to the next line mytype temp temp = x x = y y = temp

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
myswap.h: #define mySwap( mytype, x, y ) { \ the "\" continues the definition to the next line mytype temp; \ temp = x; \ x = y; \ y = temp; \ } myprogram.c #include "mySwap.h" int main() { int a; int b; a = 5; b = 4; printf( "a = %d b = %d\n", a, b); mySwap( int, a, b ); printf( "a = %d b = %d\n", a, b); } tmp.c #define myswap(mytype,x ,y) int main() { printf(…) //expansion of my swap { int tmp; tmp = a; a = b; b = tmp } printf(……) } then gcc compiles tmp.c that generates the program Problems with #define for complex macros
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
-difficult to debug. int tmp; myswap(int, tmp, b); int this case it expands to int tmp; tmp=tmp; tmp is already used in macro. -the preprocessor does a "brute-force" substitution Templates is a feature of c++ that does a "smarter" substitution. Steps to convert a class into a template: 1. write the class without templates and debug it and test it 2. templates are written in a.h file only. they normally are not implemented in a.cc file 3.before the struct and class definitions add
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/04/2012 for the course CS 251 taught by Professor Staff during the Fall '08 term at Purdue University-West Lafayette.

Page1 / 3

Cs251-week5 - myswap.h#define mySwap mytype x y cfw the continues the definition to the next line mytype temp temp = x x = y y = temp

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online