开发者

Questions about const and ifdef [duplicate]

开发者 https://www.devze.com 2023-01-22 02:58 出处:网络
This question already has answers here: Closed 12 years ago. Possible Duplicates: the role of #ifdef and #ifndef
This question already has answers here: Closed 12 years ago.

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?


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消