tags:

views:

74

answers:

2
+2  A: 

I can help you on your first point. The use of #ifdef is to change what code is compiled based on previously configured information.

For example:

#define MAKE_SEVEN
#ifdef MAKE_SEVEN
    int x = 7;
#else
    int x = 9;
#endif

is _exactly the same as:

    int x = 7;

as far as the compiler is concerned.

Why is this useful? Well, one reason is that you may want different code to be compiled for different machines, while still only having one copy of the source code.

Now this "configuration" can be set with a #define or even as part of the command line for the compiler, like gcc -DMAKE_SEVEN ....

A more useful example than given above may be to use a different data type depending on the environment you're targeting. The code:

#ifdef INT_IS_32_BITS
    typedef int int32;
#else
    #ifdef LONG_IS_32_BITS
        typedef long int32;
    #else
        #error No suitable type for int32 found
    #endif
#endif

will allow you to use the correct type for a 32-bit integer type, for different architectures.

For all those an int is 32 bits, you use gcc -DINT_IS_32_BITS ..., for all those a long is 32 bits, you use gcc -DLONG_IS_32_BITS ..., for all others, the compiler will complain.

paxdiablo
ok but some times i see like
piyapiya
#if defined(CPU_INTEL) || defined(WIN32) || defined(_M_AMD64) || defined(_M_IA64)#define WKB_BYTE_ORDER wkbNDR#else#define WKB_BYTE_ORDER wkbXDR#endif.. but here i did 't define this before than why we are saying if defined this ..... than do this
piyapiya
and please tell me about const too
piyapiya
const void operator=(const point here if it say this as funtion by reference means this function say that operator = take reference of argument but it other will behave as ordinary variable inside this function and const is used inside parameter will not change the actual value of argument.. than why const is used before void...i am right that this functipon is actually function by reference.. please make it clear to me and also explain me about const why used efoer void..
piyapiya
@piyapiya, those defines you're looking at are for specific processors, operating systems and architectures. For example, you need to do something differently under Windows and Linux. As to the `const` question, I'll leave that to someone with more C++-fu than I.
paxdiablo
and realy thanks for helping me Sir..
piyapiya
can you please make it easy for me about #ifdef.. now here
piyapiya
#ifdef INT_IS_32_BITS typedef int int32;#else #ifdef LONG_IS_32_BITS typedef long int32; #else #error No suitable type for int32 found #endif#endif
piyapiya
now here before this INT_IS_BITS is not defined already than how can we say that ifdef INT_IS_BITS.. than do this.. and you means by defining directory i can use on different windows... is it? and if use it in main for compiler time than it will not be complied for other machine.. is it?
piyapiya
It's defined in the compiler command line. Compiling with `gcc -DXYZZY` is the same as having `#define XYZZY` at the top of your source file.
paxdiablo
ohh you means we use #ifdef like above mentioned although its not defiend by our self.. actually thats defined by compiler.. right on the base of this we apply condition and define our own defination right...
piyapiya
Sometimes these constants are defined in headers that you #include as well.
bta
i have an example like
piyapiya
#if defined(CPU_INTEL) || defined(WIN32) || defined(_M_AMD64) || defined(_M_IA64)#define WKB_BYTE_ORDER wkbNDR #else #define WKB_BYTE_ORDER wkbXDR#endif like this
piyapiya
here CPU_INTEL WIN#@ M_AMD64 and one more its already defined by compiler so we sued it in if conditions thats will be sued nby compiler of its true by preprocessor...
piyapiya
is it? if yes than what si preprocessor..
piyapiya
@piyapiya, that line you posted is figuring out the byte order to use (big-endian or little-endian) based on whether the platform is INTEL/WIN32/AMD64/IA64 or otherwise. That's all it does, change (based on the hardware/OS) another define that will be used later on.
paxdiablo
hmm right i got it
piyapiya
now one more question #ifndef Swap#define Swap(typ, a, b) { typ t = (a); (a) = (b); (b) = t; }#endif
piyapiya
here ifndef i know that in my header file its not already defined than why we say ifndef... and inside defien parameter there we say type and within in {} we use type t.. is that mean we are creating a varable.. we should say int t.. something like this
piyapiya
@piyapiya, that would be better asked as a _separate_ question since it's unrelated to `#ifdef`, and comments are meant for notes rather than questions and answers. What you have there is a macro definition.
paxdiablo
A: 

Not sure why this is related to pdf-generation......

When you pass arguments to a method or function, and when those arguments are passed by reference or by pointer, those arguments can be changed by the called routine.

(const char *other) or (const point &other) indicate to the caller that you don't modify the objects referenced (or pointed at) by the passed parameter.

To indicate that a method of an object does not modify the object, you follow the method declaration with const.

class X {
    private:
        char *name;
    public:
        Draw() const;        // Draw does not modify X
        const char*getName() const { return name; }
}

If a const object is returned from a function, the caller of the function isn't allowed to modify the returned value. This is useful when you are returning a member you don't want changed. Above, getName returns a pointer that can't be used to modify the name member parameter.

I could tell you about const_cast<> too, but well, children shouldn't play with matches....

Mark0978
i got that buts till confused.. please try to make it clear to me by const void operator=(const point
piyapiya
const void operator=(const point
piyapiya
here const inside parameter is sued to not modofy the arguments. and this func is by ref that meas thsis function will take argument as reference and inside function parameter will behave as ordinaryy variables.. and const inside parameter shows that jit will not change the parameter is it
piyapiya
if its like this than why cosnt is used before void.. what willl do it
piyapiya
may be may AL questions will be funny for you all peoples but to be very franks.. there is no age for study. and if some one wan than we should courage him or her.. right
piyapiya