Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. static constant string

static constant string

Scheduled Pinned Locked Moved Managed C++/CLI
questionc++performancetutorial
2 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    VizOne
    wrote on last edited by
    #1

    Hi there! I have just started using managed c++ (until now I have always used "pure" C++), so I have a question that might sound stupid to you. I want my class to have a string-member that descibes the class. This string is for example returned by ToString(). As a C++ I would want to make it static const to ensure it is only once in memory and cannot be altered. However, ToString() returns a non-const String. Do I have to copy the string all the time I call ToString()? Maybe some code shows what I mean: public __gc class Foo { public: System::String * ToString(); private: static const System::String * ClassID = S"This is class Foo"; }; System::String * Foo::ToString() { //This won't work because return value is non-const //return Foo::ClassID; //This won't work either, as callee might manipulate string // return const_cast(Foo::ClassID); // The only possibility? return static_cast(const_cast(Foo::ClassID)->Clone()); } Are there any other ways to avoid copying all the time? Greetings Andre (VizOne) Loker

    V 1 Reply Last reply
    0
    • V VizOne

      Hi there! I have just started using managed c++ (until now I have always used "pure" C++), so I have a question that might sound stupid to you. I want my class to have a string-member that descibes the class. This string is for example returned by ToString(). As a C++ I would want to make it static const to ensure it is only once in memory and cannot be altered. However, ToString() returns a non-const String. Do I have to copy the string all the time I call ToString()? Maybe some code shows what I mean: public __gc class Foo { public: System::String * ToString(); private: static const System::String * ClassID = S"This is class Foo"; }; System::String * Foo::ToString() { //This won't work because return value is non-const //return Foo::ClassID; //This won't work either, as callee might manipulate string // return const_cast(Foo::ClassID); // The only possibility? return static_cast(const_cast(Foo::ClassID)->Clone()); } Are there any other ways to avoid copying all the time? Greetings Andre (VizOne) Loker

      V Offline
      V Offline
      VizOne
      wrote on last edited by
      #2

      Someone already gave me the clue. MSDN says: "String Class. Represents an immutable series of characters." Stupid me! RTFM... - Andre

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups