Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Is this valid?

Reply
Thread Tools

Is this valid?

 
 
Michael
Guest
Posts: n/a
 
      10-27-2004
Hi i recently came across some code that looked like this which I've never
ever seen before! I appreicate it may be 'c' but is it valid in c++??


//main.c

int main()
{
int a;
int b;
//Get values for a & b;

if(a==1)
{
#include "DoA.c"
}
else
{
#include "DoB.c"
}

// More stuff


return 0;
}


//DoA.c
b =4;


//DoB.c
b=6;



Is this valid? Or just obscure?


 
Reply With Quote
 
 
 
 
Ioannis Vranos
Guest
Posts: n/a
 
      10-27-2004
Michael wrote:

> Hi i recently came across some code that looked like this which I've never
> ever seen before! I appreicate it may be 'c' but is it valid in c++??
>
>
> //main.c
>
> int main()
> {
> int a;
> int b;
> //Get values for a & b;
>
> if(a==1)
> {
> #include "DoA.c"
> }
> else
> {
> #include "DoB.c"
> }
>
> // More stuff
>
>
> return 0;
> }
>
>
> //DoA.c
> b =4;
>
>
> //DoB.c
> b=6;
>
>
>
> Is this valid? Or just obscure?



It is valid.



--
Ioannis Vranos

http://www23.brinkster.com/noicys
 
Reply With Quote
 
 
 
 
Matt Wharton
Guest
Posts: n/a
 
      10-27-2004
"Michael" <(E-Mail Removed)> wrote in message
news:clp7u1$dje$(E-Mail Removed)...
> Hi i recently came across some code that looked like this which I've never
> ever seen before! I appreicate it may be 'c' but is it valid in c++??
>
>
> //main.c
>
> int main()
> {
> int a;
> int b;
> //Get values for a & b;
>
> if(a==1)
> {
> #include "DoA.c"
> }
> else
> {
> #include "DoB.c"
> }
>
> // More stuff
>
>
> return 0;
> }
>
>
> //DoA.c
> b =4;
>
>
> //DoB.c
> b=6;
>
>
>
> Is this valid? Or just obscure?
>
>


Yes, it's valid. Yes, it's obscure (and entirely unnecessary in the example
you posted).

-Matt


 
Reply With Quote
 
Michael
Guest
Posts: n/a
 
      10-28-2004
in that case, does #include mean copy contents of file here ( with the
absence of pragma's)?

"Matt Wharton" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Michael" <(E-Mail Removed)> wrote in message
> news:clp7u1$dje$(E-Mail Removed)...
> > Hi i recently came across some code that looked like this which I've

never
> > ever seen before! I appreicate it may be 'c' but is it valid in c++??
> >
> >
> > //main.c
> >
> > int main()
> > {
> > int a;
> > int b;
> > //Get values for a & b;
> >
> > if(a==1)
> > {
> > #include "DoA.c"
> > }
> > else
> > {
> > #include "DoB.c"
> > }
> >
> > // More stuff
> >
> >
> > return 0;
> > }
> >
> >
> > //DoA.c
> > b =4;
> >
> >
> > //DoB.c
> > b=6;
> >
> >
> >
> > Is this valid? Or just obscure?
> >
> >

>
> Yes, it's valid. Yes, it's obscure (and entirely unnecessary in the

example
> you posted).
>
> -Matt
>
>



 
Reply With Quote
 
Jonathan Turkanis
Guest
Posts: n/a
 
      10-28-2004

"Michael" <(E-Mail Removed)> wrote in message
news:clpffo$k93$(E-Mail Removed)...
> in that case, does #include mean copy contents of file here ( with the
> absence of pragma's)?


It means exactly the same thing as it does when used at the beginning of a file.
The preprocessor knows nothing about control structures such as if ( ) { }else
{ }, and so has no idea that headers are being #include'd in unusual places.

The ability to #include headers in the middle of a block of code is very useful
for preprocess metaprogramming (see
http://boost-consulting.com/tmpbook/preprocessor.html)

Jonathan


 
Reply With Quote
 
Ioannis Vranos
Guest
Posts: n/a
 
      10-28-2004
Michael wrote:

> in that case, does #include mean copy contents of file here ( with the
> absence of pragma's)?



#includes are processed by the preprocessor before the compilation. In
the final source code that gets compiled, the #includes have been
replaced by the text that they include.



--
Ioannis Vranos

http://www23.brinkster.com/noicys
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments