std::basic_stringchar or std::string

Started by
4 comments, last by Nitage 16 years, 1 month ago
Last night I was finishing up a project for college when I was looking for a way to use std::string much like I would a variable in any class.

class.h

class CClass {
	.
	.
	.
	int *var1, *var2;
	std::string *str1, *str2;
	.
	.
	.
};

class.cpp

CClass::CClass() {
	var1 = new int;
	str1 = new std::string
	.
	.
	.
}
That worked find for awhile until I needed to use some of the strings member functions:

std::string::iterator iter;
for (iter = str.begin(); iter < str.end(); iter++)
{
	tempstr.push_back(tolower(*iter));
}
Now that's the final code I came up with because *str.begin() wouldn't work saying something along the lines of not being a valid member function and just declaring str as std::string str; in my header file wasn't available in the scope of my class members. It only worked when I declared it like so:

std::basic_string<char> str;
std::basic_string<char> tempstr;
Basically what I'd like to know is am I doing anything wrong? I've done some searches on basic_string and all I come up with is the functions templates so I'm assuming no body actually uses it. In which case how would I make a std::string data type available for use in all my class members? Granted this was late at night so I'm sure I missed something, so any help on this matter would be greatly appreciated.
Advertisement
Why does your class hold it's member variables by pointer rather than value? I think this is the cause of your problem.
Well, the obvious problem that jumps out at me is that you have declared str as a pointer to an std::string, and yet when trying to access its begin() and end() members, you use the '.' syntax instead of the '->' syntax. Is there any reason why you need a pointer to a string?
Mike Popoloski | Journal | SlimDX
str->begin()


Then again, it's very rare that you'll allocate std::string on a heap. The whole point of standard containers is that you allocate them on stack, so that compiler takes care of entire memory management.

class foo {public:  foo() {} // no explicit construction  foo(const std::string & s1, const std::string & s2)    : str1(s1)    , str2(s2)  {}  foo(const char * s1, const char * s2)    : str1(s1)    , str2(s2)  {}           // no destructor, not neededprivate:  std::string str1;  std::string str2;};int main() {  foo f1;  // f1 has two usable strings  foo f2("Hello", "World"); // str1 and str2 are initialized to s1 and s2  return 0;} // f1 and f2 and its strings are properly de-allocated


Quote:I've done some searches on basic_string and all I come up with is the functions templates so I'm assuming no body actually uses it.


Everyone who uses std::string uses it. std::string is a typedefed std::basic_string< char, std::char_traits<char>, std::allocator<char> > (or close to that)

basic_string is used for template programming.
// accepts std::string, std::wstring or any variation of them,// using any allocatortemplate < class C, class T, class A >void foo( const std::basic_string<C, T, A> & s ){  // do foo on s};
This is compile-time polymorphism, where you can pass any string type, but each type will be treated individually
I forgot about the '->' operator.

However, I was unsure how to be able to use std::string in my whole class without assigning it to heap, I kinda became dependent on it. Just one of those things where I learn one way to program then I forget about the 17 better ways to do it.

Thanks for the help. I think I got it now.
Ok, now that you've got it working - does your class have a destructor, assignment operator and copy constructor?

This topic is closed to new replies.

Advertisement