Results 1 to 3 of 3

Thread: Strings in Visual C++

  1. #1
    Flower Child stytagm's Avatar
    Join Date
    Aug 2004
    Location
    London
    Posts
    754
    Thanks
    47
    Thanked
    23 times in 18 posts

    Question Strings in Visual C++

    Well there's nothing like starting with a biggie, my first real, grown up post:

    I'm using M$ Visual C++ 6 (Because I got a free copy with my textbook, and I'm still a student) and am trying to make a GUI app to run on windows. It's a classic student exercise, simple banck account type affair, bit of inheritance, bit of file IO (not allowed to use a database). Just a class with a load of attributes and set and get functions, with all the instances either in the file, and/or in a linked list. The GUI pretty much just displays the lists and lets you fill in attributes.

    Anyway the point: I have to use strings for most of teh attrobutes, name, address etc, and am having dificulty with them. c-style char arrays would be fine, but they seem to be a real pain to use when you have to verify data, for example make sure there are no characters you don't want that might muck things up.

    The STL (Standard template library) strings look like they would be good, but Microsoft doesn't seem to like supporting them.

    Microsofts MFC CString seems to be very polarised, people either love it, or hate it. I'm prepared to love it, but can't get them to work. I've got a function that is supposed to return a CString, which I then try to assign to another CString I have ready, but I get a compiler error saying that it cannot convert from a CString to a Char*.

    Two things about that error irk me, one I'm not trying to, I'm trying to assign one CString object the value of anotherCString object. And secondly, there should be an overloaded assignment operator for CString to Char* (c-style char arrays), surely that's pretty elimentary. Unless Microsoft are trying to avoid bloat, which isn't very characteristic.

    Right, sorry about the length and complexity of that, Please let me know if I've exceeded the bounds of this category.

    Also, please don't tell me to use Borland, GCC or whatever. Visual C++ is what I've got, and it's probably the easiest thing to write a windose GUI in. I doubt I'll write another for years.

    Thanks,

    Andrew.
    They told me I was gullible ... and I believed them.

  2. #2
    Member
    Join Date
    Aug 2003
    Location
    Wimbledon
    Posts
    141
    Thanks
    0
    Thanked
    0 times in 0 posts
    Two things about that error irk me, one I'm not trying to, I'm trying to assign one CString object the value of anotherCString object. And secondly, there should be an overloaded assignment operator for CString to Char* (c-style char arrays), surely that's pretty elimentary. Unless Microsoft are trying to avoid bloat, which isn't very characteristic.
    You can assign one CString to another. Can you post a code snippet so we can point out the error of you ways?

    For CString there is no operator char*() but you will find there is:
    Code:
    operator LPCTSTR() const
    and in the wonderful world of ASCII this is equivalent to:
    Code:
    operator const char* () const
    If you think about it makes good sense. The CString owns the memory that the char* points at. If the operator were not const then a third party could gain access to this memory and do nasty things to it.

    const is a great way of ensuring correctness within a program as you get to find out at compile time rather than run time.

    The stl string also does not have operator char*() for the same reason.

  3. #3
    Flower Child stytagm's Avatar
    Join Date
    Aug 2004
    Location
    London
    Posts
    754
    Thanks
    47
    Thanked
    23 times in 18 posts

    Unhappy I'm getting there, mostly I was being stupid.

    Quote Originally Posted by Mart
    You can assign one CString to another. Can you post a code snippet so we can point out the error of you ways?
    I've had a muck about, I think I was just being a bit dumb, and now I can't remember how I was doing it wrong. My assignment is no longer throwing up a compiler error, but now I think I remember my intial problem.

    if you try
    Code:
       cout << myCString;
    you get what looks like an address, eg 004219EC, which come to think of it looks more like a hex value. You know as I'm typing this a little voice in my head says I've been being a bit silly. but...
    Code:
       cout << (LPCTSTR)myCString;
    works fine.

    So to use a CString do you pretty much allways need to use (LPCTSTR), which gives you effectively a c-style string from your CString object?
    Also, does this mean that a CString basicaly is a c-style string, just with a library of functions tacked on? I think I need to read up on it, the microsoft help pages just seem to take me roudn in circles and have obviously got me well confused, I'll google around for a guide.
    Quote Originally Posted by Mart
    If you think about it makes good sense. The CString owns the memory that the char* points at. If the operator were not const then a third party could gain access to this memory and do nasty things to it.
    I see the logic here yes, I should have noticed that it was coming back const char*. However I would have thought it was my responsibility when I write my function to ensure that noone can modify it, hence my set and get functions return by value, not reference. Doesn't return by value normaly produce a copy of the object in memory, which is returned to the calling function?

    I'm sorry for asking such newby questions, I get the feeling I really should know this, but it's been several months, and these days I'm mostly doing requirements engineering (sob).

    I'll keep tinkering and if I'm still stuck tomorrow, I'll try and refine my question until it makes sense.

    Thanks, Andrew.
    They told me I was gullible ... and I believed them.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Making a Calculator in Visual Basic
    By mike_w in forum Software
    Replies: 16
    Last Post: 17-06-2008, 03:50 PM
  2. Visual Studio 2005 Beta 1
    By Shad in forum Software
    Replies: 4
    Last Post: 30-07-2004, 09:05 AM
  3. Logging into a Website through a Visual Basic App
    By MurphmanL in forum Software
    Replies: 5
    Last Post: 25-05-2004, 09:39 PM
  4. Visual Basic Noob Alert!!
    By MurphmanL in forum Software
    Replies: 2
    Last Post: 04-04-2004, 05:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •