std::string question.

Oct 19, 2012 at 8:25am
Hello, I am curious if it is possible to create a string without using the 'new' keyword and access this string with pointers of other objects. Is it possible to keep a string even though the object that created it is deleted? Where are std::strings stored in memory?
An example of what I want to do(using the 'new' keyword) is the following.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <string>


class StringKeeper
{
public:
	StringKeeper(std::string a_Data)
		{
			m_Data = new std::string (a_Data);
		}
	~StringKeeper()
		{
			delete m_Data;
		}
	void PrintString()
		{
			std::cout << *m_Data << std::endl;
		}
protected:
	std::string* m_Data;
};
int main()
{
StringKeeper one("Cat");
one.PrintString();
return 0;
}
Oct 19, 2012 at 8:36am
Hello, I am curious if it is possible to create a string without using the 'new' keyword and access this string with pointers of other objects.
Yes, using copy:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <string>


class StringKeeper
{
public:
	StringKeeper(std::string a_Data)
		{
			m_Data = a_Data;
		}
	~StringKeeper()
		{
		}
	void PrintString()
		{
			std::cout << m_Data << std::endl;
		}

	const std::string &GetData() const
		{
			return m_Data;
		}
protected:
	std::string m_Data;
};
int main()
{
std::string s;
{
StringKeeper one("Cat");
one.PrintString();
s = one.GetData();
}
std::cout << s << std::endl;
return 0;
}


Where are std::strings stored in memory?
You can't tell, why is int relevant?
Oct 19, 2012 at 8:57am
Well I wanted to access the string even after the StringKeeper object destruction. I know I can do it if I dynamically allocate it with 'new' keyword and then skip 'delete' in the destructor, but I was curious if there is any other way.
Oct 19, 2012 at 9:24am
You can either copy a pointer to an object or the object itself. miracles are (as far as I know) not included in C++ (maybe in Java).

what you can do to improve the handling of pointers is using smart pointers:

http://en.wikipedia.org/wiki/Smart_pointer

due to the reference counting the object stays alive until it's not longer referenced. But that'll still involves new
Oct 19, 2012 at 11:10am
It is simpler to rewrite method GetData the following way (by removing the reference)

1
2
3
4
std::string GetData() const
{
	return m_Data;
}


In this case the code snip will work without any problem

1
2
3
4
5
6
7
std::string s;
{
	StringKeeper one("Cat");
	one.PrintString();
	s = one.GetData();
}
std::cout << s << std::endl;
Oct 19, 2012 at 3:54pm
Hmm ok, thank you.
But my main question was if there is any way to create an std::string inside the class but store it outside of the class (but not in the heap). So even if the object that created the string gets destructed another object that will have the address of the string will be able to access the string.
Topic archived. No new replies allowed.