473,486 Members | 2,162 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

const BUGS me down!

I am sure all of you are familier with this example from effective C++:
What exactly is the problem and more IMPRTANTLY why?
is there something (more like everything) I don't understand about
"string" ? I have been having some problems with this in my own code as
well. THX

String someFamousAuthor() // randomly chooses and
{ // returns an author's name
switch (rand() % 3) { // rand() is in <stdlib.h>
// (and <cstdlib> - see
// Item 49)
case 0:
return "Margaret Mitchell"; // Wrote "Gone with the
// Wind," a true classic
case 1:
return "Stephen King"; // His stories have kept
// millions from sleeping
// at night
case 2:
return "Scott Meyers"; // Ahem, one of these
} // things is not like the
// others...
return ""; // we can't get here, but
// all paths in a value-

Jul 23 '05 #1
8 1283
puzzlecracker wrote:

I am sure all of you are familier with this example from effective C++:
Please don't assume that.
Even if you assume that give more information about what you are
referring to. "Effective C++" contians a lot of things. So which
item are you referring to?
What exactly is the problem and more IMPRTANTLY why?
Well. What are the smyptoms?
What error message do you get?
is there something (more like everything) I don't understand about
"string" ? I have been having some problems with this in my own code as
well. THX

String someFamousAuthor() // randomly chooses and
There is no such class called 'String' in standard C++
{ // returns an author's name
switch (rand() % 3) { // rand() is in <stdlib.h>
// (and <cstdlib> - see
// Item 49)
case 0:
return "Margaret Mitchell"; // Wrote "Gone with the
// Wind," a true classic
case 1:
return "Stephen King"; // His stories have kept
// millions from sleeping
// at night
case 2:
return "Scott Meyers"; // Ahem, one of these
} // things is not like the
// others...
return ""; // we can't get here, but
// all paths in a value-

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #2
puzzlecracker wrote:
I am sure all of you are familier with this example from effective C++:
All those who have read it.
What exactly is the problem and more IMPRTANTLY why?
is there something (more like everything) I don't understand about
"string" ? I have been having some problems with this in my own code as
well. THX

String someFamousAuthor() // randomly chooses and

I assume you want it to be string someFamousAuthor()

{ // returns an author's name
switch (rand() % 3) { // rand() is in <stdlib.h>
// (and <cstdlib> - see
// Item 49)
case 0:
return "Margaret Mitchell"; // Wrote "Gone with the
// Wind," a true classic
case 1:
return "Stephen King"; // His stories have kept
// millions from sleeping
// at night
case 2:
return "Scott Meyers"; // Ahem, one of these
} // things is not like the
// others...
return ""; // we can't get here, but
// all paths in a value-

What is the problem here?

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #3
puzzlecracker wrote:
I am sure all of you are familier with this example from effective C++:
What exactly is the problem and more IMPRTANTLY why?
is there something (more like everything) I don't understand about
"string" ? I have been having some problems with this in my own code as
well. THX

String someFamousAuthor() // randomly chooses and
The class is "string" not String.
{ // returns an author's name
switch (rand() % 3) { // rand() is in <stdlib.h>
// (and <cstdlib> - see
// Item 49)


Look up srand.
Jul 23 '05 #4
puzzlecracker wrote:
I am sure all of you are familier with this example from effective C++:
What exactly is the problem and more IMPRTANTLY why?
is there something (more like everything) I don't understand about
"string" ? I have been having some problems with this in my own code as
well. THX

String someFamousAuthor() // randomly chooses and
{ // returns an author's name
switch (rand() % 3) { // rand() is in <stdlib.h>
// (and <cstdlib> - see
// Item 49)
case 0:
return "Margaret Mitchell"; // Wrote "Gone with the
// Wind," a true classic
case 1:
return "Stephen King"; // His stories have kept
// millions from sleeping
// at night
case 2:
return "Scott Meyers"; // Ahem, one of these
} // things is not like the
// others...
return ""; // we can't get here, but
// all paths in a value-

Jul 23 '05 #5

"Ioannis Vranos" <iv*@remove.this.grad.com> wrote in message
news:1114524730.652339@athnrd02...
puzzlecracker wrote:
I am sure all of you are familier with this example from effective C++:
All those who have read it.


And remember everything they've read. :-)

What exactly is the problem and more IMPRTANTLY why?
is there something (more like everything) I don't understand about
"string" ?
It's not talking about "string", it's talking about "String", the class he's
demonstrating a problem with.
I have been having some problems with this in my own code as
well. THX

String someFamousAuthor() // randomly chooses and

I assume you want it to be string someFamousAuthor()


(No..actually, that's a class Mr. Meyers has defined to help demonstrate the
point he's trying to make.)

{ // returns an author's name
switch (rand() % 3) { // rand() is in <stdlib.h>
// (and <cstdlib> - see
// Item 49)
case 0:
return "Margaret Mitchell"; // Wrote "Gone with the
// Wind," a true classic
case 1:
return "Stephen King"; // His stories have kept
// millions from sleeping
// at night
case 2:
return "Scott Meyers"; // Ahem, one of these
} // things is not like the
// others...
return ""; // we can't get here, but
// all paths in a value-

What is the problem here?


To the OP (puzzlecracker)...did you read all of the text associated with
that example (Item 29 in Effective C++)? It explains the whole issue pretty
well, I thought.

The problem is related to the String class definition, and this example
demonstrates the dangers of allowing a user to obtain a pointer to internal
data of an object (the temporary String object returned by the function, in
this case).

Read the whole item again. And notice that it's *not* that function that's
the problem, but rather the method chosen to return the internal character
data (see the String class definition in that item), which makes it possible
for a calling function to attempt to refer to internal data which may
already have been destroyed.

-Howard



Jul 23 '05 #6
it describes the consequences but not the problem:

""""""""""
Now consider this use of someFamousAuthor, in which we assume that
String declares an operator const char* member function as described
above: ¤ Item E29, P25

const char *pc = someFamousAuthor();
cout << pc; // uh oh...
""""""""""""
for smart ones: there is NO NO NO class called string - - string is
actually a typedef for an instantiation of a class template. And to
make it clearer:

namespace std {
template<class charT, class traits =
char_traits<charT>,
class Allocator=allocator<charT> >
class basic_string;

;
typedef basic_string<char> string;
}

Jul 23 '05 #7

"puzzlecracker" <ir*********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
it describes the consequences but not the problem:

Read the entire item, including the text *before* that example. It does
explain the problem. It's the operator() that returns a char* which
provides a "handle" to the internal data member. That's what he's saying
you need to avoid.
""""""""""
Now consider this use of someFamousAuthor, in which we assume that
String declares an operator const char* member function as described
above: $ Item E29, P25

const char *pc = someFamousAuthor();
cout << pc; // uh oh...
""""""""""""
for smart ones: there is NO NO NO class called string - - string is
actually a typedef for an instantiation of a class template. And to
make it clearer:

namespace std {
template<class charT, class traits =
char_traits<charT>,
class Allocator=allocator<charT> >
class basic_string;

;
typedef basic_string<char> string;
}

--------------

Umm, ok. And your point is? There is a template class. The template has
an instantiation, which is thus a class. And a typedef provides an alias
for that class. So std::string is a class, which happens to be an
instantiation of a template class. So what's the problem?

-Howard



Jul 23 '05 #8
puzzlecracker wrote:

it describes the consequences but not the problem:
Huch. How come?
If I read that item, it clearly says what the problem is.
It also tells you how this problem arises (the sequence of
events that lead to that problem) and what you can do about it:

<Quote>
Believe it or not, you can't predict what this code will do, at least not with any certainty.
That's because by the time you try to print out the sequence of characters pointed to by pc,
that sequence is undefined. The difficulty arises from the events that transpire during the
initialization of pc: ¤ Item E29, P26

A temporary String object is created to hold someFamousAuthor's return value. ¤ Item E29, P27
That String is converted to a const char* via String's operator const char* member function,
and pc is initialized with the resulting pointer. ¤ Item E29, P28
The temporary String object is destroyed, which means its destructor is called. Within the
destructor, its data pointer is deleted (the code is shown in Item 11). However, data points
to the same memory as pc does, so pc now points to deleted memory — memory with undefined
contents. ¤ Item E29, P29

Because pc was initialized with a handle into a temporary object and temporary objects are
destroyed shortly after they're created, the handle became invalid before pc could do
anything with it. For all intents and purposes, pc was dead on arrival. Such is the
danger of handles into temporary objects. ¤ Item E29, P30

For const member functions, then, returning handles is ill-advised, because it violates
abstraction. Even for non-const member functions, however, returning handles can lead
to trouble, especially when temporary objects get involved. Handles can dangle, just
like pointers, and just as you labor to avoid dangling pointers, you should strive to
avoid dangling handles, too. ¤ Item E29, P31

Still, there's no reason to get fascist about it. It's not possible to stomp out all
possible dangling pointers in nontrivial programs, and it's rarely possible to eliminate
all possible dangling handles, either. Nevertheless, if you avoid returning handles when
there's no compelling need, your programs will benefit, and so will your reputation. ¤ Item E29, P32

</Quote>

""""""""""
Now consider this use of someFamousAuthor, in which we assume that
String declares an operator const char* member function as described
above: ¤ Item E29, P25

const char *pc = someFamousAuthor();
cout << pc; // uh oh...
""""""""""""


--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #9

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

Similar topics

10
7467
by: Joachim Schmitz | last post by:
Hi folks Is it legal for a C compiler that claims to be conforming to the standard (c89) to issue an error on the following: char *foo(const char *s) { const char *s; for (s = src; *s &&...
17
11194
by: Janice | last post by:
char* line = "abcd"; How to convert the line to upper case and print? Any option for printf to do this? Thanx
3
1454
by: Markus.Elfring | last post by:
How much effort do you invest to develop a const-correct design? Did a later addition of the type modifier "const" for a function interface uncover any real errors and bugs in your source code at...
0
471
by: tom olson | last post by:
See the following related link: http://gcc.gnu.org/ml/gcc-bugs/1999-10/msg00173.html >-----Original Message----- >I have C++ code that has been running just fine in VS6 for >a couple of...
13
1420
by: Peteroid | last post by:
Why does reading a member of a std::map not considered const? For example: class My_Class { int Get_Map_Value( int index ) const // ** error ** not considered const!!! { return m_Map ; //...
20
2412
by: Snis Pilbor | last post by:
Whats the point of making functions which take arguments of a form like "const char *x"? It appears that this has no effect on the function actually working and doing its job, ie, if the function...
17
2410
by: Adrian Hawryluk | last post by:
Hi all, What is everyone's opinion of const inheriting? Should the object that a pointer is pointing at inherit the constness of the pointer? Such as in the case of a class having a pointer...
2
1906
by: Angus | last post by:
I have a member function, int GetLogLevel() which I thought I should change to int GetLogLevel() const - I made the change and it works fine. But in the function I am creating buffers and of...
8
1540
by: puzzlecracker | last post by:
void func(const set<string&); It is typical for pointers, but not reference to pass as const if you don't want modified. Who finds himself doing it above -- passing the reference as const?
0
7100
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
6964
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
7330
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...
1
4865
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...
0
4559
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
3070
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
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
262
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...

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.