String class problem

Started by
8 comments, last by Corrail 20 years, 8 months ago
Hi all! I''ve written my own String struct with constructor, destructor, operators, ... using a simple char array but I''ve got a problem passing the String to a function:

void test(String xxx)
{
  // ....

  // ....

} // <- Destructor Call with deleting my char-Array


void main()
{
  String myString;
  test(myString);
  // Now the charecter array of my string is deleted because of

  //destructor call in void test()

}
It works with with passing a pointer (of course, because there''s no call to the dextructor) but is there any other way?? Thanks a lot Corrail corrail@gmx.at ICQ#59184081
--------------------------------------------------------There is a theory which states that if ever anybody discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable.There is another theory which states that this has already happened...
Advertisement
?? i think copy construktor will help You
"The Gods Made Heavy Metal And They Saw That It Was Good They Said To Play It Louder Than Hell We Promised That We WouldWhen Losers Say Its Over With You Know That It's A Lie The Gods Made Heavy Metal And It's Never Gonna Die"THE GODS MADE HEAVY METAL/by ManOwaR
I''m not 100% sure I understand, but it sounds like you''re having it delete your string when you don''t want it to? This is because you''re passing by value to the function. As you noted, passing by reference or pointer doesn''t have this problem. You can solve the pass by value problem by adding a copy constructor to your class that allocates new data and copies that data being pointed to so when the function returns, it deletes its own copy instead of the one from main(). I hope that makes sense. I believe a copy constructor looks like this, but I''m not positive: String::String(const String &x)

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


You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming

You are unique. Just like everybody else.

"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

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

You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

The Rule of Three: If you need to define a destructor, an assignment operator, or a copy constructor, you most likely need to define all three.

How appropriate. You fight like a cow.
Solved it using a copy contructor!
Thanks a lot for the information!! :-)

Corrail
corrail@gmx.at
ICQ#59184081
--------------------------------------------------------There is a theory which states that if ever anybody discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable.There is another theory which states that this has already happened...
You really should listen to Sneftel, if you''re adding a copy constructor, you also need an assignment operator. Get a copy of "Effective C++", it will fully explain why.

Tony

Assigment operator is defined

I''ev got this book at home but didn''t start to read it yet... *gg*

"If it looks good, it is good"
"If it looks like computer graphics, it is not good computer graphics"

Corrail
corrail@gmx.at
ICQ#59184081
--------------------------------------------------------There is a theory which states that if ever anybody discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable.There is another theory which states that this has already happened...
also you should have this, to simplifi things:
operator const char*()const{  return this->c_string;} 
I think the AP has the right idea, but there''s some weirdness to his code... for one, it doesn''t have the class name scoping thingy (I forget the technical term). Also, I''ve seen more and more lately that people are accessing their class''s member variables by differencing the this pointer. What''s up with that? Seems inefficient and needlessly complex to me...

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


You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming

You are unique. Just like everybody else.

"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

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

You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

quote:Original post by Anonymous Poster
also you should have this, to simplifi things:
operator const char*()const{  return this->c_string;}  

No no no no no no no. Providing implicit casts to built-in types is one of those things that sounds like a good idea at the time, but eventually comes back to bite you in the ass. This is especially true if you''re casting to a pointer, which is an iterator. Let''s say you had a vector of these strings, and you were iterating through the container, applying an STL algorithm to each. If you stick an extra dereference on the iterator, the compiler won''t flag an error, but bad things will happen. Add this to the ambiguity that results when overloading other functions with respect to the string class, and you''ll see why the STL doesn''t use a single cast-operator, especially not for std::string.

How appropriate. You fight like a cow.

This topic is closed to new replies.

Advertisement