want to return a constant reference to a...

K

Kent

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
 
K

Karl Heinz Buchegger

Kent said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
anuragag27

Latest Threads

Top