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
String class problem
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:
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
--------------------
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
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.
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
Thanks a lot for the information!! :-)
Corrail
corrail@gmx.at
ICQ#59184081
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
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
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
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
--------------------
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
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
Popular Topics
Advertisement