473,670 Members | 2,419 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

return const and assert

When to return a const, from either a member function of a class or
non-member?

Is assert() only executed in debug mode but skipped in release mode? I
don't see much usage of "assert()." Am I missing its importance?

Thanks for your comments!
Jul 22 '05 #1
4 2066
"alexhong20 01" <al**********@h otmail.com> wrote...
When to return a const, from either a member function of a class or
non-member?
When returning by value, 'const' really isn't of any use. It's
basically like adding 'const' when passing by value: means almost
nothing.
Is assert() only executed in debug mode but skipped in release mode?
Using your terminology, yes.
I
don't see much usage of "assert()." Am I missing its importance?


Probably. 'assert' is a debugging tool for code where you don't
want to have a run-time check once you cleaned everything up.

V
Jul 22 '05 #2
alexhong2001 wrote:
When to return a const, from either a member function of a class or
non-member?
Call these "top level const":

void function1(int const q);
int const function2();

Don't do that. The 'const' has no meaning to the calling code, because both
values pass by copy. The qualification (const, non-const, volatile,
non-volatile) of the copy are irrelevant.

But do this:

class foo {
int bar;
public:
int const & getBar() { return bar; }
};

Do that so those who access foo::bar cannot change its value. The /Effective
C++/ books cover this.
Is assert() only executed in debug mode but skipped in release mode?
If your implementation' s "release mode" defines NDEBUG, then assert() goes
away. But this newsgroup is not qualified to discuss "release mode", because
the configurations that your implementation provides by default are
implementation-specific. Questions about "release mode" will get the best
answers on your compiler's newsgroup.
I don't see much usage of "assert()." Am I missing its importance?


Assert() is the most important function in the whole programming industry.
You won't see it much in the tutorial code. But consider this simple
example:

int main()
{

Source aSource("a b\nc, d");

string
token = aSource.pullNex tToken(); assert("a" == token);
token = aSource.pullNex tToken(); assert("b" == token);
token = aSource.pullNex tToken(); assert("c" == token);
token = aSource.pullNex tToken(); assert("d" == token);
token = aSource.pullNex tToken(); assert("" == token);
// EOT!
}

Passing the test requires objects of type Source to parse strings, ignoring
spaces and commas. If you add assertions to test cases like this, get them
to fail, and then edit your source to make the tests pass, you can grow
programs of any size and complexity, without the need to ever operate your
debugger. Run the tests after every 1~10 edits, and only perform the kinds
of edits that immediately return a program to a state where all the tests
still pass.

That technique trades long hours debugging for short minutes writing tests.

--
Phlip
http://www.xpsd.org/cgi-bin/wiki?Tes...UserInterfaces
Jul 22 '05 #3


alexhong2001 wrote:

I don't see much usage of "assert()." Am I missing its importance?


This is a function to determine the number of steps required to
approximate the portion of a curve, between parametric values t_start
and t_end, to a given tolerance.

int Curve::num_step s(
double t_start,
double t_end,
double eps
) const
{
CHECKVALID_CLAS S;
ASSERT_STATE(!i nvalid(),"Curve ::num_steps");
ASSERT_STATE(!i nfinite(),"Curv e::num_steps");
ASSERT_ARGUMENT (eps>0.0,"Curve ::num_steps");
ASSERT_ARGUMENT (t_min()<=t_sta rt, "Curve::num_ste ps");
ASSERT_ARGUMENT (t_end>=t_start , "Curve::num_ste ps");
ASSERT_ARGUMENT (t_end<=t_max() , "Curve::num_ste ps");

// compute the number of spans to match given tolerance.
const int nspans = utMath::ceiling ((t_end - t_start) *
utMath::sqrt(de riv2_max(t_star t,t_end)/(8.0*eps)));
// make sure we have at least one span.
return nspans<1 ? 1 : nspans;
}

As you can see assertions of one form or another dominate the function.
Asserts help you to build reliable systems, detect and zero in on bugs
quickly, and provide the mechanism for writing automatic tests.

Jul 22 '05 #4
> Is assert() only executed in debug mode but skipped in release mode? I
don't see much usage of "assert()." Am I missing its importance?


Checkout the following article:

http://www.eventhelix.com/RealtimeMa...y_contract.htm

Sandeep
--
http://www.EventHelix.com/EventStudio
EventStudio 2.0 - System Architecture Design CASE Tool
Jul 22 '05 #5

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

Similar topics

19
16290
by: Robert | last post by:
Greetings everyone, I was wondering if a const variable or object took up space. I know that a #define'd macro doesn't, as it's basically just interpreted by the compiler. If a const does take up space, is there any reason to choose it over a #define'd constant? -- Thank you P.S. if it makes any difference, I ssh to a SunOs machine where I use
4
2619
by: Eric | last post by:
I have read that using const_cast to modify an object that was originally declared const can lead to undefined behavior. Would this be true in the case of a user defined object containing a const data member as in the example below, and if so, why? In what cases can modification of an originally declared const object be problematic? I would appreciate any comments. struct TestClass
13
19350
by: Vijay Kumar R. Zanvar | last post by:
Hello, I have few questions. They are: 1. Is "const char * const *p;" a valid construct? 2. How do I align a given structure, say, at 32-byte boundary? 3. Then, how do I assert that a given object is aligned, say, at 32-byte boundary?
10
1532
by: ATASLO | last post by:
In the following example, section #3 fails under VC98, VC2003, VC2005 Express Beta (Aug 2004) and g++ 3.3.2. Is this just a pitfall of the C++ specification? Why don't any of the above compilers at least flag this as a warning as they would when say trying to return a const & to a local? In Section #2, the const B& Bref is initialized and bound to the temporary returned from GetSettings(). That is the temporary B exists until Bref goes...
17
2444
by: benben | last post by:
Given a class template Vector<>, I would like to overload operator +. But I have a hard time deciding whether the return type should be Vector<U> or Vector<V>, as in: template <typename U, typename V> Vector<U_or_V> operator+ ( const Vector<U>&, const Vector<V>&);
21
2681
by: Jim Langston | last post by:
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<unsigned int, CMap*>::iterator ThisMapIt = World.Maps.find( ThisPlayer.Character.Map ); if ( ThisMapIt != World.Maps.end() )
3
1325
by: cpp | last post by:
I have the following code class A { private: string s1; string s2; string s3; string s4;
173
8085
by: Marty James | last post by:
Howdy, I was reflecting recently on malloc. Obviously, for tiny allocations like 20 bytes to strcpy a filename or something, there's no point putting in a check on the return value of malloc. OTOH, if you're allocating a gigabyte for a large array, this might fail, so you should definitely check for a NULL return.
39
2782
by: Leonardo Korndorfer | last post by:
Hi, I'm litle confused by the const modifier, particularly when use const char* or char*. Some dude over here said it should be const char when you dont modify it content inside the function, I read somewhere that it when you won't modify after its initialization... So when exactly do I use one or another? Is it *wrong* not use const when I should?
0
8469
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8386
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
8903
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...
0
8661
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
7419
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, 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...
1
6213
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5684
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();...
2
2042
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1794
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.