want to return a constant reference to a...

Discussion in 'C++' started by Kent, Nov 10, 2003.

  1. Kent

    Kent Guest

    Hi!

    I am working with an obscure graphics library in school as a university C++
    programming project. The graphics library has a class kalled Bitmap wich you
    use to draw graphics (you can "stamp it down" on the screen, double
    buffering and such is all within the lib).

    This is how it works now (i think its a bad design):

    In a class i created to represent "monsters" in a game i have the following
    method (se declaration on line below):

    Bitmap* getBitmap(void); // returns the pointer i store localy

    The class has a variable wich points to an instance of a Bitmap (se line
    below):
    Bitmap* bitmappointer; // Points to the picture of the monster

    I need to return the bitmappointer in order to be able to draw it (wich is
    handled by another method), but when i give out the bitmap pointer something
    outside my class instance could modify it and make it corrupt or destroy it.

    So, i am wonder how i should solve this? Can i return a "constant reference"
    instead so that the data stored at the pointer position couldn´t be harmed
    by accident? And if so, how do i implement it? (in other words, what does
    the return look like)

    That was all, hope that i described my problem in enough detail for you to
    understand it. And that you managed to look past my grammatical errors and
    typos (as english isn´t my native language i can just do my best)

    Best regards and many thanks in advance! / Kent
    Kent, Nov 10, 2003
    #1
    1. Advertising

  2. Kent wrote:
    >
    > Hi!
    >
    > I am working with an obscure graphics library in school as a university C++
    > programming project. The graphics library has a class kalled Bitmap wich you
    > use to draw graphics (you can "stamp it down" on the screen, double
    > buffering and such is all within the lib).
    >
    > This is how it works now (i think its a bad design):
    >
    > In a class i created to represent "monsters" in a game i have the following
    > method (se declaration on line below):
    >
    > Bitmap* getBitmap(void); // returns the pointer i store localy
    >
    > The class has a variable wich points to an instance of a Bitmap (se line
    > below):
    > Bitmap* bitmappointer; // Points to the picture of the monster
    >
    > I need to return the bitmappointer in order to be able to draw it (wich is
    > handled by another method), but when i give out the bitmap pointer something
    > outside my class instance could modify it and make it corrupt or destroy it.
    >
    > So, i am wonder how i should solve this? Can i return a "constant reference"
    > instead so that the data stored at the pointer position couldn´t be harmed
    > by accident? And if so, how do i implement it? (in other words, what does
    > the return look like)


    You could.

    you have a function

    ... foo()

    and that function returns a reference ...

    ... & foo()

    ...., a reference to a bitmap ...

    Bitmap & foo()

    .... and that bitmap is constant

    const Bitmap & foo ()

    Or of course you could do the same with a pointer:

    const Bitmap * foo()

    foo returns a pointer ... to a Bitmap ... and that Bitmap is constant.

    Bitmap * const foo()

    foo returns something that is constant ... that something is a pointer ... and points to a Bitmap.

    const Bitmap * const foo()

    foo returns something that is constant ... that something is a pointer ... and points to a Bitmap
    ....
    and that Bitmap is constant too.


    const always works on the thing left to it, with the only exception if const is the leftmost
    specifier, then it works on the thing right to it:

    const int i;
    and int const i;

    are equivalent.

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Nov 11, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    7,121
    John Currier
    Jul 14, 2005
  2. Christopher M. Lusardi
    Replies:
    1
    Views:
    4,086
  3. Martin Magnusson
    Replies:
    2
    Views:
    503
    John Harrison
    Oct 8, 2004
  4. Tor Erik Soenvisen
    Replies:
    14
    Views:
    559
    Tim Roberts
    Nov 23, 2006
  5. Replies:
    4
    Views:
    338
    Keith Thompson
    Dec 14, 2006
Loading...

Share This Page