-
August 22nd, 2003, 02:26 PM
#1
an auto_ptr question
I declared:
auto_ptr<char> a(new char[20]);
My question is how I can initialize a with a string?
-
August 22nd, 2003, 02:45 PM
#2
Hi,
U need to call explicitaly strcpy to copy a string.
u can not initialize it directly.
Vinod
-
August 22nd, 2003, 02:57 PM
#3
Originally posted by vinodp
Hi,
U need to call explicitaly strcpy to copy a string.
u can not initialize it directly.
Vinod
I am sorry I don't understand what you said. Actually, strcpy doesn't take the auto_ptr<char> as a parameter type. So how could you use strcpy to copy a string to a in question?
-
August 22nd, 2003, 03:08 PM
#4
strcpy(a.get(), "Codeguru");
Beware of length of the string & allocated char length.
My suggestion is instead of this use STL::string class.
Vinod
-
August 22nd, 2003, 03:15 PM
#5
Code:
auto_ptr<char> a(new char[20]);
auto_ptr can not be used for arrays. It calls delete,
not delete [] on the object it owns.
Use one of the standard containers (std::string as
already suggested or something like :
vector<char> a(20);
then if you need to pass to a function that expects a c-style
string, you use : &a[0]
-
August 22nd, 2003, 03:46 PM
#6
Originally posted by vinodp
strcpy(a.get(), "Codeguru");
Beware of length of the string & allocated char length.
My suggestion is instead of this use STL::string class.
Vinod
I don't think strcpy(a.get(), "Codeguru") is going to work.
-
August 22nd, 2003, 03:48 PM
#7
Originally posted by Philip Nicoletti
Code:
auto_ptr<char> a(new char[20]);
auto_ptr can not be used for arrays. It calls delete,
not delete [] on the object it owns.
Use one of the standard containers (std::string as
already suggested or something like :
vector<char> a(20);
then if you need to pass to a function that expects a c-style
string, you use : &a[0]
I guess you are right. One way is like this:
auto_ptr<string> a(new string("Hello\n");
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
|