473,387 Members | 2,436 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

want to return a constant reference to a...

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
Jul 19 '05 #1
1 9950


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
kb******@gascad.at
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Maximus | last post by:
Hi, I was just wondering, is it good to use return without arguments in a void function as following: void SetMapLayer() { if( !Map ) return; layer = LAYER_MAP; }
19
by: Christian Engström | last post by:
If you have a function that returns something by value, the gcc compiler (version 3.2.3 on Windows XP with MinGW) converts the returned value from the type you specify in the code, to the const...
8
by: DaKoadMunky | last post by:
Please consider the following... <CODE> #include <string> using namespace std; typedef int PrimitiveType; typedef string ClassType;
3
by: red floyd | last post by:
Once again, I'm fighting a port from the (allegedly standard compliant) VC7.1 to G++. VC compiles this, G++ doesn't. Am I allowed to pass the anonymous temporary returned by f() to a function...
6
by: amparikh | last post by:
I know this is something fundamental and I ought to have known it, but somehow this seems to be confusing me a lot. Fundamentally, rvalues and/or temporaries can be bound only to constant...
29
by: pmatos | last post by:
Hi all, Sometimes I have a function which creates an object and returns it. Some are sets, other vectors but that's not very important. In these cases I do something like this: vector<int> *...
31
by: CeZaR | last post by:
Hi, How can i specify the return type of a function returning a managed array of chars. If i try to write: "char __gc func()" i get an error! How can i do that? Thanks!
68
by: Jim Langston | last post by:
I remember there was a thread a while back that was talking about using the return value of a function as a reference where I had thought the reference would become invalidated because it was a...
2
by: subramanian100in | last post by:
I am reading David Musser's "STL Tutorial and Reference Guide" Second Edition. In that book, on pages 68-69, definition has been given that "an iterator can be mutable or constant depending on...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.