473,396 Members | 2,014 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,396 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 9954


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: 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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.