|
 |
|
 |
06-27-2005, 08:08 PM
|
#1 (permalink)
|
|
Registered User
Join Date: Jun 2005
Posts: 26
|
using a string in a class
Thankies to Valmont for being so helpful in fixing the problem that has led me to this problem!
Now that I know how to use and create header files, have got everything linking correctly, and figured out this #ifndef stuff, I'd like to....use a string as a variable type in one of my classes.
So-oo, how do I do this? I've sort of been skirting my string issues for awhile now. I don't really understand anything about the 'using namespace std;' statement, which is the only way I've been getting strings to work. Now that I've got my class seperated into a .h and .cpp file, I don't know where I would put it, either.
I've tried putting it in various places and messing around alot but I keep getting lots of "'string' used as a type, but not defined as a type" errors.
Can someone guide me through using a string as a var type in a class? Thanks.
|
|
|
06-27-2005, 10:04 PM
|
#2 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
You'll need to include the header <string> wherever you declare a std::string object (variable).
About namespaces (like std) see this thread:
return types
After reading this, I want you (to test yourself) to write me a simple function swapping two std::string objects. You must do this in your own namespace. Post it. Even in case of a failure. Then we'll take a look at it together.
__________________
|
|
|
06-28-2005, 08:08 PM
|
#3 (permalink)
|
|
Registered User
Join Date: Jun 2005
Posts: 26
|
First of all, a question about the original topic (to post the namespace thing in a sec...)
I can get strings to work just fine using std::string in any of my derived classes. However, if I try and use it in my base class, I get this 'undefined reference to MyClass::MyClass(bool)' error. Is there something different about the way this works with base classes, or shall I post the code?
Thanks,
-Stacy
|
|
|
06-28-2005, 08:21 PM
|
#4 (permalink)
|
|
Registered User
Join Date: Jun 2005
Posts: 26
|
Thanks for the explanation and the exercise. Here's what I've got:
Code:
#include <iostream>
#include <string>
namespace stacy
{
//swap() swaps string1 and string2, then prints the new values.
void swap(std::string string1, std::string string2)
{
std::string holder = "This will temporarily hold a string for me.";
holder = string1;
string1 = string2;
string2 = holder;
std::cout << "String 1 is now '" << string1 << "' and string 2 is '" << string2 << "'.";
}
}
int main()
{
stacy::swap("one", "two");
std::cin.get();
}
This works fine, but I'm wondering about something: A while back, someone (who was doing Java; I don't know if this still applies...) was telling me that assigning an object to another (holder = string1) can cause problems because one object has two labels(?) At the end of swap(), holder and string2 are the same. Do I need to/should I do something about this?
|
|
|
06-28-2005, 10:26 PM
|
#5 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,705
|
You might be refering to this thread where you would parse by reference instead of making copies like your swap function does.
Which would alter your code to be:
Code:
#include <iostream>
#include <string>
namespace stacy
{
//swap() swaps string1 and string2, then prints the new values.
void swap(std::string &string1, std::string &string2)
{
std::string holder;
holder = string1;
string1 = string2;
string2 = holder;
std::cout << "String 1 is now '" << string1
<< "' and string 2 is '" << string2 << "'."
<< std::endl;
}
}
int main()
{
std::string one = "one";
std::string two = "two";
std::cout << "Befor swap: " << one << " " << two << std::endl;
stacy::swap(one, two);
std::cout << "After swap: " << one << " " << two << std::endl;
std::cin.get();
}
|
|
|
06-29-2005, 01:19 AM
|
#6 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Yes, the pass by reference is important. However, the standard output does not belong in the function swap. Let the function do only what its name implies.
Anyway, here is how it looks when more people work at the same project in real life:
Code:
#include <iostream>
#include <string>
namespace stacy
{
//swap() swaps two strings.
void swap(std::string& string1, std::string& string2)
{
std::string holder;
holder = string1;
string1 = string2;
string2 = holder;
}
}
namespace Alex
{
//swap() swaps two strings.
void swap(std::string* string1, std::string* string2)
{
std::string holder;
holder = *string1;
*string1 = *string2;
*string2 = holder;
}
}
int main()
{
std::string one("one"), two("two"), three("three");
//Entering namespace scope.
{
using namespace stacy;
swap(one, two);
}
//Exiting namespace scope
//---------------------------------//
//Entering namespace scope.
{
using namespace Alex;
swap(&two, &three);
}
//Exiting namespace scope
std::cout << "After swap: " << one << " " << two <<" "<< three << std::endl;
std::cin.get();
}
__________________
|
|
|
06-29-2005, 03:45 AM
|
#7 (permalink)
|
|
Registered User
Join Date: Jun 2005
Posts: 3
|
Ironically, strings include their own swap method which is much more efficient -- it takes a constant time of operation instead of copying every character. For example:
Code:
std::string a = "Alpha";
std::string b = "Beta";
a.swap(b);
std::cout << a << ' ' << b << std::endl;
This prints Beta Alpha.
|
|
|
06-29-2005, 07:03 AM
|
#8 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,705
|
Quote:
|
Ironically, strings include their own swap method
|
The object of this thread, was to learn the usage of namespaces and object oriented C++, by creating your own implementation of some standard provided features.
Quote:
|
which is much more efficient - it takes a constant time of operation instead of copying every character.
|
Then compare it to the version, where you parse by reference.. You'll soon notice they are the same.
|
|
|
06-29-2005, 05:50 PM
|
#9 (permalink)
|
|
Registered User
Join Date: Jun 2005
Posts: 3
|
Quote:
|
Originally Posted by redhead
Then compare it to the version, where you parse by reference.. You'll soon notice they are the same.
|
Huh? Are you talking about this?
Code:
void swap(std::string& string1, std::string& string2)
{
std::string holder;
holder = string1;
string1 = string2;
string2 = holder;
}
This is not the same as the string class's swap method. The string class's swap method simply swaps the objects' pointers that point to their dynamically allocated arrays, which means that no matter how large the strings are, the operation takes the same amount of time.
The code above copies characters around -- if string1 and string2 are fifty characters long, then fifty characters will get copied from string1 to holder, then fifty characters will get copied from string2 to string1, and then fifty characters will get copied from holder to string2. Also, holder will have to perform a memory allocation when it gets assigned from string1.
This is far more expensive than the swap method, which only has three pairs of pointers to swap, everytime, with no extra allocations in between.
|
|
|
06-29-2005, 07:53 PM
|
#10 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Besides that, a destructor needs to be called for the temporary variable as well. The std::string has its own swap() method indeed. It is usually between 6 or 7 times as faster as the methods presented earlier.
Nevertheless, there is no "irony" involved in the kind of assignments presented earlier. Often the student needs to learn to develop algorithms at an early stage. Re-inventing the wheel is a common thing.
__________________
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 03:56 PM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|