473,396 Members | 1,785 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.

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 2053
"alexhong2001" <al**********@hotmail.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.pullNextToken(); assert("a" == token);
token = aSource.pullNextToken(); assert("b" == token);
token = aSource.pullNextToken(); assert("c" == token);
token = aSource.pullNextToken(); assert("d" == token);
token = aSource.pullNextToken(); 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_steps(
double t_start,
double t_end,
double eps
) const
{
CHECKVALID_CLASS;
ASSERT_STATE(!invalid(),"Curve::num_steps");
ASSERT_STATE(!infinite(),"Curve::num_steps");
ASSERT_ARGUMENT(eps>0.0,"Curve::num_steps");
ASSERT_ARGUMENT(t_min()<=t_start, "Curve::num_steps");
ASSERT_ARGUMENT(t_end>=t_start, "Curve::num_steps");
ASSERT_ARGUMENT(t_end<=t_max(), "Curve::num_steps");

// compute the number of spans to match given tolerance.
const int nspans = utMath::ceiling((t_end - t_start) *
utMath::sqrt(deriv2_max(t_start,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
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...
4
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...
13
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...
10
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...
17
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,...
21
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: ...
3
by: cpp | last post by:
I have the following code class A { private: string s1; string s2; string s3; string s4;
173
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....
39
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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
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.