473,465 Members | 1,538 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

returning const reference and compiler warning

Just wondered what you guys do when you need a method that returns a
const reference to some data based upon some value.

something like this:

const CData& MyClass::GetDataWithValue(int iValue)const
{
for (int idx=0; idx<m_iNumRecords; ++idx)
{
if (m_Records[idx].GetValue() == iValue)
{
return m_Records[idx];
}
}
}

Most compilers will give a warning here. Something like "Warning: Not
all paths return a value"

This is fair enough but occasionally i know that this method will
*always* work correctly because there is no possible way iValue could
be anything else but valid. I'm certain this sort of thing crops up
for other programmers also. So my Q is: What do you do about it? (I
just ignore the warning at the moment but it irritates me!)

Aug 4 '05 #1
5 1445
Dylan wrote:
Just wondered what you guys do when you need a method that returns a
const reference to some data based upon some value.

something like this:

const CData& MyClass::GetDataWithValue(int iValue)const
{
for (int idx=0; idx<m_iNumRecords; ++idx)
{
if (m_Records[idx].GetValue() == iValue)
{
return m_Records[idx];
}
}
}

Most compilers will give a warning here. Something like "Warning: Not
all paths return a value"

This is fair enough but occasionally i know that this method will
*always* work correctly because there is no possible way iValue could
be anything else but valid. I'm certain this sort of thing crops up
for other programmers also. So my Q is: What do you do about it? (I
just ignore the warning at the moment but it irritates me!)


I'd optimize the loop if you can guarantee that you always get a
result.
in this case:

int idx;
for (idx =0; m_records[idx].getValue != iValue; ++idx);
return m_records[idx];

Oh and that ; at the end of the for is not a mistake

Aug 4 '05 #2
Dylan wrote:
Just wondered what you guys do when you need a method that returns a
const reference to some data based upon some value.

something like this:

const CData& MyClass::GetDataWithValue(int iValue)const
{
for (int idx=0; idx<m_iNumRecords; ++idx)
{
if (m_Records[idx].GetValue() == iValue)
{
return m_Records[idx];
}
}
If you are certain that in a valid program the execution should never
come here, you need to (a) assert that:

assert( ! "Reached the point where value is invalid");

and (b) return a reference to, say, a static object. If everything goes
well, and your program never reaches this point, the object isn't even
initialised, and only its storage is zero-initialised:

static CData d;
return d;
}

Most compilers will give a warning here. Something like "Warning: Not
all paths return a value"

This is fair enough but occasionally i know that this method will
*always* work correctly because there is no possible way iValue could
be anything else but valid. I'm certain this sort of thing crops up
for other programmers also. So my Q is: What do you do about it?
See above.
(I
just ignore the warning at the moment but it irritates me!)


And it should. If the code falls through, it has undefined behaviour if
the function that has non-void return type doesn't have a corresponding
'return' statement.

V
Aug 4 '05 #3
ve*********@hotmail.com wrote:
[...]
I'd optimize the loop if you can guarantee that you always get a
result.
in this case:

int idx;
for (idx =0; m_records[idx].getValue != iValue; ++idx);
... m_records[idx].GetValue() != iValue ...
return m_records[idx];

Oh and that ; at the end of the for is not a mistake


To make sure people understand it you should either comment it, or put it
on the next line, or both. Alternatively, your code can be rewritten in
an easier to understand variation:

int idx = 0;
while (m_records[idx].GetValue() != iValue)
++idx;
return m_records[idx];

V
Aug 4 '05 #4
Dylan sade:
Just wondered what you guys do when you need a method that returns a
const reference to some data based upon some value. something like this:
Precondition:
m_iNumRecords contains an object whose GetValue() == iValue
const CData& MyClass::GetDataWithValue(int iValue)const
{
for (int idx=0; idx<m_iNumRecords; ++idx)
{
if (m_Records[idx].GetValue() == iValue)
{
return m_Records[idx];
}
}
}

If an event do arise where the precondition can't be secured, I would
consider this a dangerous function, well I do anyway. At least throw
an exception at the end to signal a precondition violation for
debugging purposes.
Most compilers will give a warning here. Something like "Warning: Not
all paths return a value" This is fair enough but occasionally i know that this method will
*always* work correctly because there is no possible way iValue could
be anything else but valid. I'm certain this sort of thing crops up
for other programmers also. So my Q is: What do you do about it? (I
just ignore the warning at the moment but it irritates me!)


Even though I'm sure all data will always be valid in such a
case, I still don't blindly rely on it. A bug elsewhere can propagate
throughout the code structure and cause caos in other areas considered
secure due to naivity. Beware!

Tobias
--
IMPORTANT: The contents of this email and attachments are confidential
and may be subject to legal privilege and/or protected by copyright.
Copying or communicating any part of it to others is prohibited and may
be unlawful.
Aug 4 '05 #5
ve*********@hotmail.com wrote:

I'd optimize the loop if you can guarantee that you always get a
result.
in this case:

int idx;
for (idx =0; m_records[idx].getValue != iValue; ++idx);
return m_records[idx];

Oh and that ; at the end of the for is not a mistake


The way I prefer to write these is:

int idx;
for (idx = 0 ; m_records[idx].getValue() != iValue; ++idx)
/* DO NOTHING */ ;
return m_records[idx];

This way, it's clear that I explicitly intended an empty loop body,
and I don't have to add that parenthetical comment that you made.
Aug 4 '05 #6

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

Similar topics

18
by: cppaddict | last post by:
Hi, Is it considered bad form to have the subscript operator return a const reference variable? If not, what is the proper way to do it? My question was prompted by the code below, my...
3
by: Sree | last post by:
Hello All, When will it be benefical to return a value from a function as a const reference ? Say I have classes like this class B { public: int someValue1; int someValue2;
3
by: shaun | last post by:
I have a function which does not modify a private data member which is a map, but simply returns one value. I'd like to make that member function const, like so: int MapTest:: getValueAt(const...
10
by: d3x0xr | last post by:
---- Section 1 ---- ------ x.c int main( void ) { char **a; char const *const *b; b = a; // line(9)
6
by: EvilOldGit | last post by:
const Thing &operator++(int) { Thing temp = *this; operator++(); return temp; } Is this code robust ? I get a compiler warning about returning a reference to a a local, which I guess is...
23
by: =?iso-8859-1?q?Santiago_Urue=F1a?= | last post by:
Hi, I tried to return a pointer to a constant string, but the compiler gives the following warning if a cast is not used: warning: assignment from incompatible pointer type This is the code:
7
by: Thomas Lenz | last post by:
Please consider the following code snippet: string myfunction() { ostringstream oss; oss << "junk"; // do something more with oss; I can't make it const... return oss.str(); } Is the...
7
by: pauldepstein | last post by:
#include <iostream> using namespace std; double & GetWeeklyHours() { double h = 46.50; double &hours = h; return hours; }...
13
by: asm23 | last post by:
Hi,I need some help to clarify the warning "initial value of reference to non-const must be an lvalue". I'm searching in this groups to find someone has the same situation like me. I found in...
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
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...
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,...
1
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
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.