Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Convert from CString to string?

Posted on 2003-02-25
14
Medium Priority
?
304 Views
Last Modified: 2007-11-27
Hello,

is there an function that converts object from the CString class to object of the string class?

tnx
0
Comment
Question by:Cozzn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +4
14 Comments
 
LVL 1

Expert Comment

by:Jogy
ID: 8015594
Hello,

You can use the LPCTSTR conversion operator of CString like that:

CString str("I'm a string");

std::string s((LPCTSTR)str);


Jogy



0
 

Author Comment

by:Cozzn
ID: 8015895
Hey,

the solution for me was even simpler then that:

CString cstr="blabla";
string str=cstr;

This works fine!

tnx anyway
0
 

Expert Comment

by:entropy12
ID: 8029405
string str(cstr); is safer
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:bkrahmer
ID: 8031574
entropy12, please tell us why this is safer?
brian
0
 

Expert Comment

by:entropy12
ID: 8033243
supposedly if the cstring is not null terminated or something, the other string will assume a very long length
0
 
LVL 2

Expert Comment

by:bkrahmer
ID: 8034983
Entropy12, I encourage you to understand more about the std::string constructor, the operators that CString has and their behavior.  By design, what you are saying will not happen.  I'd like to see you prove me wrong, though.  :)
brian
0
 

Expert Comment

by:entropy12
ID: 8035336
i just noticed that you were talking about a CString and not a c-string. it was late and i must have gotten confused. sorry
0
 
LVL 2

Expert Comment

by:bkrahmer
ID: 8038922
Right.  Dealing with char[]'s is always problematic.  That's why it's nice to have classes like string that handle the termination for you.
brian
0
 

Expert Comment

by:vinay_krishna
ID: 8063769
Simply pass object of cString class to the constructor of String class .
Cstring Cs = new Cstring(arguments);
String s = new CString(Cs);

In the constructor of class String , Intialise The s with cs Objct.

Constructor:
 String(Cstring cs){
s =cs;
                   }
0
 
LVL 2

Expert Comment

by:bkrahmer
ID: 8069735
-2 points for vinay.  He/she didn't free the memory they allocated.  :)  Cozzn, this question was already answered, can you please close it?

brian
0
 

Expert Comment

by:vinay_krishna
ID: 8078834
Dear bkrahmer,
             It should be allready understood that memory sshould be deallocated finally .
0
 
LVL 2

Expert Comment

by:bkrahmer
ID: 8092406
It's called humor dude.  That's what the :) means.  Chill ims.

brian
0
 
LVL 11

Expert Comment

by:bcladd
ID: 9845973
No comment has been added lately, so it's time to clean up this TA. I will
leave a recommendation in the Cleanup topic area that this question is:

Answered by original poster: Archive and refund points

Please leave any comments here within the next seven days.

Experts: Silence means you don't care. Grading recommendations are made in light
of the posted grading guidlines (http://www.experts-exchange.com/help.jsp#hi73).

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

-bcl (bcladd)
EE Cleanup Volunteer
0
 

Accepted Solution

by:
Chmod earned 0 total points
ID: 9892262
PAQed, with points refunded (50)

Chmod
Community Support Moderator
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question