-1

Possible Duplicates:
the role of #ifdef and #ifndef
How many and which are the uses of “const” in C++?

I am new to this field and I am just not getting certain functions or concepts.

For example, I have this code:

#ifdef directory

Now, we could use a simple if condition in the program. So why is #ifdef used? I don't get it.

I also have a question about const. I have studied some papers, but they just explain how to use it but I did not find any reason why.

For example, this is not clear to me:

const void operator=(const point& other);

Here, I think this is a function called by reference. That means this function says that operator = takes a reference to its argument so other will behave as ordinary variable inside this function. And const is used to indicate that the parameter will not change the actual value of the argument. But then why is const used before void?

Community
  • 1
  • 1
piyapiya
  • 177
  • 1
  • 1
  • 8
  • 1
    wrong tag, wrong english, wrong everything – Lachezar Oct 28 '10 at 00:07
  • 2
    The first part of your question is a duplicate of another question on [the role of #ifdef and #ifndef](http://stackoverflow.com/questions/3744608/the-role-of-ifdef-and-ifndef). The second part of your question is a duplicate of another question: [How many and which are the uses of “const” in C++?](http://stackoverflow.com/questions/455518/how-many-and-which-are-the-uses-of-const-in-c) – Daniel Pryden Oct 28 '10 at 00:15

2 Answers2

2

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
  • 854,327
  • 234
  • 1,573
  • 1,953
  • #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 Oct 28 '10 at 00:13
  • and please tell me about const too – piyapiya Oct 28 '10 at 00:14
  • const void operator=(const point& other); 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 Oct 28 '10 at 00:15
  • 1
    @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 Oct 28 '10 at 00:17
  • can you please make it easy for me about #ifdef.. now here – piyapiya Oct 28 '10 at 00:25
  • #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 Oct 28 '10 at 00:26
  • 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 Oct 28 '10 at 00:29
  • 1
    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 Oct 28 '10 at 00:31
  • 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 Oct 28 '10 at 00:43
  • Sometimes these constants are defined in headers that you #include as well. – bta Oct 28 '10 at 00:46
  • #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 Oct 28 '10 at 01:00
  • 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 Oct 28 '10 at 01:02
  • is it? if yes than what si preprocessor.. – piyapiya Oct 28 '10 at 01:03
  • @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 Oct 28 '10 at 01:04
  • now one more question #ifndef Swap #define Swap(typ, a, b) { typ t = (a); (a) = (b); (b) = t; } #endif – piyapiya Oct 28 '10 at 01:09
  • 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 Oct 28 '10 at 01:11
  • @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 Oct 28 '10 at 02:27
0

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....

boatcoder
  • 17,525
  • 18
  • 114
  • 178
  • i got that buts till confused.. please try to make it clear to me by const void operator=(const point& other); – piyapiya Oct 28 '10 at 00:49
  • const void operator=(const point& other); – piyapiya Oct 28 '10 at 00:49
  • 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 Oct 28 '10 at 00:53
  • if its like this than why cosnt is used before void.. what willl do it – piyapiya Oct 28 '10 at 00:54
  • 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 Oct 28 '10 at 00:57