Thread: memset on stl string
-
November 10th, 2005 04:37 PM #1Member
-
Join Date
- Nov 2002 Location
- Karachi Pakistan Posts
- 53
memset on stl string
Hi
I have used memset for initializing values to 0 for primitive type like integers floats chars but I have problems when I try to do the same with stl strings consider this
Code:struct foo { public: int a; char c; string s; float f; }; class bar { protected: foo f; public: bar() { memset(&cdr,0,sizeof(f)); } }; int main() { bar *b = new bar(); delete b; return 0; }
Regards
Ahmed Bhaila -
November 10th, 2005 04:43 PM #2Member +
-
Join Date
- Jan 2001 Posts
- 588
Re: memset on stl string
Simply stated, you can't do that. You can't use C-style functions meant for primitive types on non-primitive types. Period. That's what constructors are for.
-
November 10th, 2005 04:50 PM #3Elite Member Power Poster
-
Join Date
- Apr 1999 Posts
- 26,306
Re: memset on stl string
Originally Posted by cyberizen
Bottom line is that you can't use memset on non-POD classes. You need to code a legitimate constructor for the foo object once you introduce non-POD members such as std::string.
Regards,
Paul McKenzie -
November 10th, 2005 04:50 PM #4Member
-
Join Date
- Nov 2002 Location
- Karachi Pakistan Posts
- 53
Re: memset on stl string
ok so this simple means that If I have say like many integers and other primitive types and a single string the only way to initialize them is by equating every element to 0 one by one
转载:http://forums.codeguru.com/showthread.php?364259-memset-on-stl-string