problem code isolated but makes no sense

Started by
13 comments, last by Dave Hunt 18 years, 10 months ago
Quote:Original post by JTR1080
Quote:Original post by Zahlman
C++ provides tools for dealing with this sort of thing in a way that's easy and *works*. In particular, it provides std::string (which also avoids problems with needing a particular buffer length).


I think that is my problem, the constructor for the surface with the overloaded
value of char* handleName, sets that char* to whatever the address in memory
handleName is, which is probably tileBitmap array so when it gets changed to
the new variable it changes the handleSurface. I'm changing the members of Csurfaces to have char intstead of char* to hold the data, the only problem is converting between the two so i have to write some annoying code like
*** Source Snippet Removed ***
hopefully it works

edit: problem solved! :)


No, the problem as was mentioned by Zahlman is that you are using char's and char *'s when you should be using std::string. There really isn't many reasons now adays when you actually want to use a char *. It really is in your best interest to learn how to use std::string. It's way easier to handle and can be converted to a char * if needed to by some archaic api function.

You are basically doing a lot of work to work around a problem that already has a valid answer. Please consider reading up on std::string.
Advertisement
Quote:Original post by Lacutis
No, the problem as was mentioned by Zahlman is that you are using char's and char *'s when you should be using std::string.


No, the problem was exactly as stated - assigning the address of the source to the destination, rather than making a copy. The "best" solution may be to use std::string instead, but that isn't the "problem". It is perfectly possible to get his code working without std::string. Less clean, but not impossible.
char* something = something_else;

This will make something a pointer to something_else. You want to copy the data instead. In windows the following code will work like a charm:

strcpy( something, something_else );

Alternatively you could write a function that loops through both strings and copies the data over, this however requires assembly language to be done at a speed comparable to strcpy.

** edit **
Don't let using c style strings scare you, click here instead. [cool]
Quote:Original post by Dave Hunt
Quote:Original post by Lacutis
No, the problem as was mentioned by Zahlman is that you are using char's and char *'s when you should be using std::string.


No, the problem was exactly as stated - assigning the address of the source to the destination, rather than making a copy. The "best" solution may be to use std::string instead, but that isn't the "problem". It is perfectly possible to get his code working without std::string. Less clean, but not impossible.


I've been doing maintenance work recently... please don't tell me that "un-clean" code isn't a "problem". :)

Quote:Original post by Zahlman
I've been doing maintenance work recently... please don't tell me that "un-clean" code isn't a "problem". :)


I wouldn't dream of it. I've dealt with other people's (and my own) unclean code for 25 years.

Now that the OP's code is working, perhaps he has learned something about pointers. Hopefully he will take a look at std::string for future use. But, he needed a solution to an immediate problem, not a lecture on the virtues of the standard C++ library.

This topic is closed to new replies.

Advertisement