Click here to Skip to main content
15,902,893 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Simple C to C++ Pin
enhzflep14-Sep-11 16:21
enhzflep14-Sep-11 16:21 
GeneralRe: Simple C to C++ Pin
Chuck O'Toole14-Sep-11 17:18
Chuck O'Toole14-Sep-11 17:18 
AnswerRe: Simple C to C++ Pin
Erudite_Eric14-Sep-11 20:41
Erudite_Eric14-Sep-11 20:41 
AnswerRe: Simple C to C++ Pin
Richard MacCutchan14-Sep-11 22:14
mveRichard MacCutchan14-Sep-11 22:14 
GeneralRe: Simple C to C++ Pin
Orjan Westin15-Sep-11 1:38
professionalOrjan Westin15-Sep-11 1:38 
GeneralRe: Simple C to C++ Pin
Richard MacCutchan15-Sep-11 1:43
mveRichard MacCutchan15-Sep-11 1:43 
AnswerRe: Simple C to C++ Pin
Orjan Westin15-Sep-11 1:32
professionalOrjan Westin15-Sep-11 1:32 
AnswerRe: Simple C to C++ Pin
Stefan_Lang15-Sep-11 1:43
Stefan_Lang15-Sep-11 1:43 
First, the original code should compile and work for any C++ compiler. Almost any C code should, as long as your function declarations contain the full parameter list (this was not mandatory in C, but is in C++). If the function doesn't work as intended in C++, then it didn't in C either!

If your intention is to refactor the code into something that more resembles C++ coding standard, here's a few pointers:

1. Do not use #define for constants. C++ introduced the const keyword for that purpose and AFAIK ANSI C as well. #define always introduces a risk, as it replaces text without concern for the context, and therefore might break your code in places that it was not meant to affect. It's even worse when #defines are used in headers, making the replacement global.

2. Do not use magic numbers. Magic numbers are numeric or string literals that are used within the code to define array boundaries, values passed to functions, or limits used for loops. It's almost always better to instead define a constant, using a name that explains its purpose or use. There are multiple advantages of doing this: First, if you ever need to change the value you only need to change it in one place, no matter how often you used it, and no matter whether others used it in places that you don't even know of; Second, the name of the constant explains what it is, saving people the effort to somehow divine it from the context or (nonexistent) comments; Third, constant names are often easier to remember than the literals they represent. And intelligent editors will even remember these names for you.

3. Use std::string instead of C-style 0-terminated strings. They are sometimes more awkward to use, but they're fast and generally more safe. They also manage their own memory, so you don't need to allocate an arbitrarily sized buffer yourself, nor do you need to care about its deallocation. Also there are already plenty of functions available in the STL, either as member functions of std::string, or as generic functions found in algorithm:: (unfortunately though, none of them exactly reciprokes your function)

4. Be careful when using index values for std::string, or in fact any of the containers of the STL. For one, many functions in the STL require iterators, not index values; most of the time index values - if provided for a container - can be used to read (or write) an element, but nothing else! Second, checking for the end (or start) of a range is often not easily possible with index values, as you have noticed yourself. The usual method for iterating oover the contents of an STL container, and that includes strings, is by using iterators.

5. Don't use break, nor multiple return statements in a function: they are just as bad as goto in that they obscure the program flow and make it unneccesarily hard to understand the code. The best way to replace a break is putting the condition into the loop header. If some of the code needs to be skipped, store the condition in a variable that is used both in the loop header and in an if statement that encapsulates the part of the loop that needs to be skipped. The important part is that the condition(s) that end a loop should always be visible in the header, so anyone trying to analyze the program flow can immediately see whether a loop will be invoked at all, and for how long. (you already rightfully asked why there is no end condition in the C-style for loop!)

6. If you need a function that works on a STL object (in this case std::string), check for functions already in existance and learn how to use them. E. g. for std::string, check out http://www.cplusplus.com/reference/string/string/[^]

With all of the above (and more) in mind, your code should like something like this (not tested):
C++
#include <string>
#include <iostream>
const std::string LESSER_THAN_SIGN = "<";      // string literal -> use constant
const std::string LESSER_THAN_HTML = "&lt;";  // string literal again
const std::string GREATER_THAN_SIGN = ">";     // and another, extending your example
const std::string GREATER_THAN_HTML = "&gt;"; // again

/**
 * replaces all occurrences of some substring with another string
 * @param text     in/out   text to be worked on
 * @param from     in       substring to be replaced
 * @param to       in       string used as replacement
*/
void replace_all(std::string& text, const std::string& from, const std::string& to) {
   // Work your way through the entire text by repeatedly searching for 'from' string.
   // We will use the text position to store the current search position rather than
   // an iterator, because the string replacement might invalidate iterators!
   std::size_t textpos = 0;                        // the first position is 0, not 1
   std::size_t replaced_length = from.length();    // number of characters of the search string
   // keep searching for the string 'from' while an occurence can be found
   // find() will return std::string::npos as a result upon failure
   while ((textpos = string.find(from, textpos)) != std::string::npos) {
      // found an occurence of 'from' to replace
      // --> replace the substring starting at textpos with the new string 'to'
      text.replace(textpos, replaced_length, to);
   }
}

int main() {
   std::string text("<b>some fat text</b>");
   replace_all(text, LESSER_THAN_SIGN, LESSER_THAN_HTML);
   replace_all(text, GREATER_THAN_SIGN, GREATER_THAN_HTML);
   std::cout << text << std::endl;
   return 0;
}

Note: I used the JavaDoc style for commenting the function. This style is also used by Doxygen, a tool that like JavaDoc can automatically extract a full function level HTML documentation from these type of formatted comments. Even if you don't use DoxyGen, it is generally a good idea to define *some* standard for commenting each function.

Sorry this turned out rather longer than intended, but I hope you will appreciate it anyway Wink | ;)
GeneralRe: Simple C to C++ Pin
Richard MacCutchan15-Sep-11 1:47
mveRichard MacCutchan15-Sep-11 1:47 
JokeRe: Simple C to C++ Pin
MicroVirus15-Sep-11 3:36
MicroVirus15-Sep-11 3:36 
QuestionMFC OpenGL Invalidate Flickering Pin
appollosputnik14-Sep-11 6:19
appollosputnik14-Sep-11 6:19 
AnswerRe: Flicker Free Drawing In MFC Pin
Software_Developer14-Sep-11 7:32
Software_Developer14-Sep-11 7:32 
AnswerRe: MFC OpenGL Invalidate Flickering Pin
Roger Allen27-Sep-11 6:23
Roger Allen27-Sep-11 6:23 
QuestionShowHTMLDialog not working in Windows 7 Pin
doug2514-Sep-11 4:00
doug2514-Sep-11 4:00 
AnswerRe: ShowHTMLDialog not working in Windows 7 Pin
Chris Meech14-Sep-11 6:07
Chris Meech14-Sep-11 6:07 
GeneralRe: ShowHTMLDialog not working in Windows 7 Pin
doug2514-Sep-11 7:39
doug2514-Sep-11 7:39 
GeneralRe: ShowHTMLDialog not working in Windows 7 Pin
Chris Meech14-Sep-11 8:27
Chris Meech14-Sep-11 8:27 
GeneralRe: ShowHTMLDialog not working in Windows 7 Pin
doug2514-Sep-11 8:59
doug2514-Sep-11 8:59 
Questionindex of member of a map? Pin
zon_cpp14-Sep-11 3:16
zon_cpp14-Sep-11 3:16 
AnswerRe: index of member of a map? Pin
«_Superman_»14-Sep-11 3:31
professional«_Superman_»14-Sep-11 3:31 
AnswerRe: index of member of a map? Pin
Chris Losinger14-Sep-11 3:37
professionalChris Losinger14-Sep-11 3:37 
QuestionWaitForSingleObject Pin
john563213-Sep-11 23:41
john563213-Sep-11 23:41 
AnswerRe: WaitForSingleObject Pin
CPallini14-Sep-11 0:12
mveCPallini14-Sep-11 0:12 
GeneralRe: WaitForSingleObject Pin
john563214-Sep-11 0:25
john563214-Sep-11 0:25 
GeneralRe: WaitForSingleObject Pin
CPallini14-Sep-11 0:39
mveCPallini14-Sep-11 0:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.