I'm sure this has been asked a few times, but I'm still not sure.
I want to create a function to simplify getting a reference to a CMap in a
map.
This is what I do now in code:
std::map<unsign ed int, CMap*>::iterato r ThisMapIt = World.Maps.find (
ThisPlayer.Char acter.Map );
if ( ThisMapIt != World.Maps.end( ) )
{
CMap& ThisMap = *((*ThisMapIt). second);
// Work with ThisMap
}
Now, the map number should always be in the map, but I'm a bit pedantic and
like to check for all possible errors. I'd like to create a function to do
this like:
CMap& FindMap( const unsigned int MapNumber )
{
std::map<unsign ed int, CMap*>::iterato r ThisMapIt = World.Maps.find (
ThisPlayer.Char acter.Map );
if ( ThisMapIt != World.Maps.end( ) )
return *((*ThisMapIt). second);
else
// What to return here? A reference can't be null!
}
My alternative is to return a CMap* and return NULL on failure, but I would
rather deal with references so I can use . instead of ->
Any suggestions?
I guess I could return a CMap* and in code do:
CMap* MapP = FindMap( ThisPlayer.Char acter.Map );
if ( MapP != NULL )
{
CMap& ThisMap = *MapP;
// Work with ThisMap
}
if I really have to
May 8 '06
21 2702
Phlip wrote: Alf P. Steinbach wrote:
* Phlip: 4) pass an optional sentinel object into FindMap, and return that. 5) return an iterator, and check if that == .end(). If the caller passes no sentinel, construct the NullObject one and return it:
CMap& FindMap( const unsigned int MapNumber, CMap const & sentinel = NullMap() );
Oh, my. That's not const-correct. What's the fix? Leave out the 'const'.
CMap & found = FindMap(2);
Now found refers to the temporary NullMap(), which destructed somewhere around ;.
Should not work - cannot bind an rvalue (temporary) to a non-constant
reference. I know it compiles under MS VC++ 7.1, but that is erroneous.
This leaves you with this option (or an exception):
....
{
std::map<unsign ed int, CMap*>::iterato r ThisMapIt =
World.Maps.find ( ThisPlayer.Char acter.Map );
if ( ThisMapIt != World.Maps.end( ) )
{
return *((*ThisMapIt). second);
}
else
{
//Same base type as CMap, but you know that!
static NullMap sentinal;
return sentinal;
}
} -- Phlip http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
Jim Langston wrote: I'm sure this has been asked a few times, but I'm still not sure.
I want to create a function to simplify getting a reference to a CMap in a map.
CMap& FindMap( const unsigned int MapNumber ) { std::map<unsign ed int, CMap*>::iterato r ThisMapIt = World.Maps.find ( ThisPlayer.Char acter.Map ); if ( ThisMapIt != World.Maps.end( ) ) return *((*ThisMapIt). second); else // What to return here? A reference can't be null! }
Instead of you map containing CMap*, I would consider using a map of
type:
std::map<unsign ed int, boost::shared_p tr<CMap> >
FindMap could then return boost::shared_p tr or boost::weak_ptr . The
advantange of this is that the client can store the pointer if he needs
to. He can't use the pointer if invalid (or at least him using it will
cause visible problems). He could also test for validity of the
returned value (No sentinal required). This he could of course do using
normal pointers too (returning NULL), but who's to say the map doesn't
change after finding the item, or who's to say the client doesn't
decide to store the map entry for some reason. I like weak_ptr in this
case because one doesn't want the item to exist after being removed
from the map which weak_ptr enforces, but shared_ptr does not..
Regards,
W
My alternative is to return a CMap* and return NULL on failure, but I would rather deal with references so I can use . instead of ->
Any suggestions?
I guess I could return a CMap* and in code do:
CMap* MapP = FindMap( ThisPlayer.Char acter.Map ); if ( MapP != NULL ) { CMap& ThisMap = *MapP; // Work with ThisMap }
if I really have to This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Krackers |
last post by:
How do you write a function which returns a reference to an array. I
can only get a function to return a copy of the array itself. I've had
a look at some other threads in this group an the return value of a
function acts like 'by Val' returning the value only (except for
objects) can you make it return a reference instead?
cheers,
Krackers
|
by: Robert Oschler |
last post by:
I have a "child" document that I use for an IFRAME element that I put into
several "parent" documents. These "parent" documents therefore contain the
IFRAME whose SRC property is set to the "child" document.
The parent(s) have three Javascript functions, here they are:
// =============================
var gMargin = 5;
|
by: Olumide |
last post by:
I'm studying Nigel Chapman's Late Night Guide to C++ which I think is
an absolutely fantastic book; however on page 175 (topic: operator
overlaoding), there the following code snippet:
inline MFVec operator+(const MFVec& z1, const MFVec& z2) // Global
function
{
MFVec res = z1;
res += z2
return res; // WHY???
|
by: Randell D. |
last post by:
Folks,
I'm sure this can be done legally, and not thru tricks of the trade - I
hope someone can help.
I'm writing a 'tool' (a function) which can be used generically in any
of my projects. When it completes, it can call a success, or a failure
function. The names of these success, or failure functions will differ,
and I'd like to know how I can pass the name of a function to my tool,
and how my tool can call the function, using that...
|
by: Martin Jørgensen |
last post by:
Hi,
I'm trying to move a matlab program into c language. For those who knows
matlab, this is the line I want to program in c:
hx(1:nx,1:ny) = 0; % nx=10, ny=10
It works on a 2-dimensional array (size is 10*10), setting all the
values inside the 10*10 matrix to zero. My C-function looks like this:
| |
by: Frederick Gotham |
last post by:
The "toupper" function takes an int as an argument. That's not too
irrational given that a character literal is of type "int" in C.
(Although why it isn't of type "char" escapes me... )
The "toupper" function imposes a further constrait in that the value
passed to it must be representable as a unsigned char. (If C does not
require all character values to be positive, then again, this constrait
too escapes me... )
Let's say we have the...
|
by: Matthew Cook |
last post by:
I would like to overload the unary minus operator so that I can negate
an instance of a class and pass that instance to a function without
creating an explicit temporary variable. Here is an example:
#include <iostream>
using namespace std;
class Object
{
public:
|
by: watkinsdev |
last post by:
Hi,
I have created a mesh class in visual studio 6.0 c++.
I can create a device, render objects and can edit the objects by for
instancnce selecting a cluster of vertices and processing the vertices
and can do this multiple times on a sinlge vertex cluster.
The problem I have been encoutering is that, if I select a second
vertex cluster and try to edit that , the program crashes.
|
by: John Doe |
last post by:
Hi,
I wrote a small class to enumerate available networks on a smartphone :
class CNetwork
{
public:
CNetwork() {};
CNetwork(CString& netName, GUID netguid):
_netname(netName), _netguid(netguid) {}
|
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,...
|
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...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |