Thread: String Literals

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    417

    String Literals

    Is there a way to make a program treat a variable like a string literal?

    i.e:

    Code:
    void func(any & x)
    {
          char * temp;
          // get data
          x = temp;
    }
    And not losing the data because it goes out of scope, sort of like this?

    Code:
    void func(any & x)
    {
          x = "temp";
    }
    (without dynamically allocating)

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    Try 'const char * temp;'. I believe that's how literals are treated in C++ normally.

    Can someone else elaborate on this, please?

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    I think I tried that, but I'm not sure... I'll try that now.

    I tried, alas, nope.

    Thanks anyway

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Magic word: static.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    static is nice...

    only its useless for this.

    this will be run multiple times, and the data inside the class can't be changed when the data is changed

  6. #6
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    So declare the data mutable and make your function a 'const' function.

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    its an ostream overload, so its not inclass

  8. #8
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Originally posted by Trauts
    its an ostream overload, so its not inclass
    It doesn't matter, it accepts a class as one of its arguments, no? Declare whatever data inside that class which will be changed as 'mutable'. eg. mutable int m_data;
    I really don't think you're explaining yourself very well... some real code would be of use right about now.

  9. #9
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    http://cboard.cprogramming.com/showt...threadid=39570

    the real problem on that post is the fact that it can't choose which operator when the bitshift ones are there... it tries to take the ostream in the template.

    I don't know how to fix that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. How to concatenate, then widen string literals
    By lonehacker in forum C Programming
    Replies: 13
    Last Post: 09-15-2004, 10:56 AM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM