static constant string
-
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 -
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