473,657 Members | 2,505 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9982


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
2660
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
2789
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 version of that type. Is this a bug that is specific to gcc, or is it a flaw in the language specification that gcc diligently implements? For example, the below program produces the output Constant Mutable
8
3072
by: DaKoadMunky | last post by:
Please consider the following... <CODE> #include <string> using namespace std; typedef int PrimitiveType; typedef string ClassType;
3
1684
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 requiring a non-const reference? I suspect G++ is correct, and VC is (again) braindead.
6
1836
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 references going by the const guidelines. Taking that into consideration, how does one get a constant reference to a pointer. class A
29
2230
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> * f() { vector<int> * v = new vector<int>; return v; }
31
2606
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
4600
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 temporary but it was stated that it would not. This has come up in an irc channel but I can not find the original thread, nor can I get any code to work. Foo& Bar( int Val ) { return Foo( Val ); }
2
4258
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 whether the result of operator* is a reference or a constant reference." As per this definition, on page 71 in this book, it is mentioned that for 'set' and 'multiset', both the iterator and const_iterator types are constant bidirectional types -...
0
8305
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8825
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8503
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4151
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4302
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1953
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1611
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.