开发者

C++ Questions: pointers/memory addresses and subclasses

Why are we allowed to run this code:

int* FunctionB(int x)
{
    int temp =30;

    //more code

    return &temp;
}

It seems to me that I am not returning what I said I would. Why is it that a memory address开发者_如何转开发 can be returned if I declared the return type to be a pointer. Isn't a pointer something that points to a memory address, not actually a memory address?


class Image : public BMP
{
    public:
    void invertcolors();
    void flipleft();
    void adjustbrightness(int r,int g,int b);

    private:
};

Upon compilation of the previous code I get this error:

image.h:3: error: expected class-name before ‘{’ token

but I thought I was using the proper syntax to declare a sub class. Is there something wrong with what I have written?


As far as your first question is concerned

Why are we allowed to run this code:

Nothing can stop you from returning the address of a local variable but that's dangerous(mark my words). Infact return address or reference of a local variable invokes Undefined Behaviour (that means anything can happen).

Also have a look at this.

Why is it that a memory address can be returned if I declared the return type to be a pointer?

That means you have to study the basics on pointers. In computer science, a pointer is a programming language data type whose value refers directly to (or "points to") another value stored elsewhere in the computer memory using its address.

..but I thought I was using the proper syntax to declare a sub class. Is there something wrong with what I have written?

Have you defined BMP or have you included the header which contains its defnition?


In the first case, a pointer is a memory address, which points to some data, so your syntax is correct. In this case, however, you're returning the address of a local variable, so it's undefined what it'll be pointing to after the function call ends.

In the second case, the compiler is complaining because it doesn't know what BMP is. You may have forgotten to #include the header file for the BMP class.


In C and C++ languages the term address and the term pointer are [almost] precise synonyms. In practical, less formal language the term address is most ofent used to refer to rvalue pointer values, while the term pointer can be seen used for both rvalue and lvalue pointer values. In your first example, you are returning an rvalue, so the terms "address" and "pointer" can be used interchangeably. You are returning a pointer to an int object. You are returning an address of an int object. Both mean the same thing. Of course, returning address of a local object makes no sense, aside form a deliberate attempt to cause undefined behavior.

As for the second question, the compiler simply doesn't know what BMP is. You didn't declare it. Moreover, a class must be defined before you can use it as a base in another class declaration.


Think about it this way:

If

char myChar;

declares a variable that store a character,

int* myPtr;

declares a variable that stores a memory address.

Analogously, if

char myCharFunction();

declares a function that returns a character,

int* myPtrFunction();

is declaring a function that returns a memory address.

Therefore, since your function's signature is int* FunctionB(int x), it is correct in returning a memory address.

The important thing to understand here is int* means "a memory address". And what we call a "pointer" is nothing but a variable that stores memory a address. When someone says a pointer "points to an address", all they mean is that the pointer holds that address in itself (just as a char variable would hold a character).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜