Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) C++98C++ Builder Exercise #8: library trouble

 

Difficulty: 2/10

Date added: 31th of May 2009

 

This exercise is only for those that use the C++ Builder 6.0 library (otherwise you will not run into the same trouble).

 

In this exercise, you learn to overcome trouble from your library and an unexpected difference between static_cast and const_cast.

 

 

 

 

 

Any programmer might want to write code like below:

 

const std::string s = "abc***def";
const int n = 3; //Number of repeats
std::search_n( s.begin(),s.end(),n,'*'); //Search for three successive asterisks

 

Programmers (like me) that use the C++ Builder 6.0 Enterprise edition IDE with the Borland compiler BCC32.EXE (version 6.0.10.157) will be in for a surprise: an unexpected compile error!

 

The compile error given is 'Cannot modify const object', because 'n' is of type 'const int'.

 

 

 

 

 

Question #0

 

Why doesn't this compile?

 

View the answer of this exercise

 

 

 

 

 

Question #1

 

It is generally a bad idea to modify standard header files. Assume you do not want to do this.

 

Which workarounds can you find to solve this problem?

 

Which one is best?

 

View the answer of this exercise

 

 

 

 

 

Post your feedback

 

Feedback can be posted at the Programmer's Heaven page about this exercise.

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict