|
-
November 28th, 2004, 01:44 AM
#1
string
Hi, I'm having some trouble using the string data type I'm very new to c++:
I would like to have a class with a non sataic data member of type string. as defined in #include <string>
Here is the jist of what I'm trying to do:
#include <string>
using namespace std;
class treeNode
{
public:
//lots of methods
private:
string nodeElement = "text";// a string that is in each node of a tree
}
-
November 28th, 2004, 03:45 AM
#2
Re: string
You can't initiliaze your string in your .H file. Do something like:
.h
Code:
#include <string>
using namespace std;
class treeNode
{
public:
//lots of methods
private:
string nodeElement;
}
.cpp
Code:
treeNode::treeNode() : nodeElement("text")
{
...
}
-
November 28th, 2004, 03:57 AM
#3
Re: string
 Originally Posted by hexaplus
#include <string>
using namespace std;
As a small side note...it is not a good idea to map the complete namespace 'std' to your application in a header file (with 'using namespace std'). Every file that includes this header will get everything out of the namespace as well which is not desirable...
The following shows you the four different methods to map a namespace...
Code:
// Using the full member name, including the namespace it belongs to:
std::cout << "Hello World";
// By taking advantage of Using-Declarations:
using std::cout; // This declares cout in the current
// scope as synonym for std::cout
cout << "Hello World";
// By taking advantage of Using-Directives:
using namespace std; // Which specifies that the current
// scope can refer to names in the
// 'std' namespace without using
// full qualifiers. This is mostly
// used when porting legacy code.
cout << "Hello World";
// Using aliases:
namespace X
{
namespace Y
{
class Z { ... };
}
}
X::Y::Z // The full qualifier for 'Z' is
// 'X::Y::Z'
namespace w = X::Y; // This declares 'w' as an alias for
// namespace 'X::Y'
w::Z // Access 'Z' using 'w::Z'
-
November 28th, 2004, 04:02 AM
#4
Re: string
 Originally Posted by Andreas Masur
As a small side note...it is not a good idea to map the complete namespace 'std' to your application in a header file (with 'using namespace std'). Every file that includes this header will get everything out of the namespace as well which is not desirable...
You are absolutely correct. I just copied/paste the code and didn't pay attention to that using namespace
-
November 28th, 2004, 05:53 PM
#5
-
November 28th, 2004, 05:58 PM
#6
like here?
so something like this in the stead:
using std::string;
-
November 28th, 2004, 07:29 PM
#7
Re: string
Nope.. Not quite. That will also pollute the global namespace. In your header, specify the std namespace explicitly on every string you declare:
Code:
std::string some_string;
In your cpp file however, you can bring in as many namespaces as you like since it will only pollute the global namespace of that particular file. (And not all files that include it like it would in a header.)
Insert entertaining phrase here
-
November 29th, 2004, 04:12 AM
#8
Re: string
 Originally Posted by wien
Nope.. Not quite. That will also pollute the global namespace.
Well...actually, it will only declare 'string' in the current scope as a synomyn for 'std::string'...in other words, it does not map the complete namespace to your application...
-
November 29th, 2004, 07:20 AM
#9
Re: string
 Originally Posted by Andreas Masur
Well...actually, it will only declare 'string' in the current scope as a synomyn for 'std::string'...in other words, it does not map the complete namespace to your application... 
Well, you do bring that "string" name in there.. It's not as bad as putting "using namespace" in the global scope, no, but should still be avoided.
However, if you put the "using" line inside the scope of an inline function, that is, not in the global scope, it's a completely different matter.
Insert entertaining phrase here
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|