C++\CLI static const System::String^ not supported?

D

DaTurk

I'm converting MC++ to C++/CLI and for some reason it's not cool with

static const System::String^

How am I supposed to have static constant strings then?
 
R

raylopez99

I'm converting MC++ to C++/CLI and for some reason it's not cool with

static const System::String^

How am I supposed to have static constant strings then?

That's weird. Perhaps you can post more code. From what I
understand, String is in fact immutable, and if you intend to mutate a
lot of strings use the more efficient StringBuilder class.

For String don't forget to always instantiate with gcnew or String
^AStr = "Some Text";

RL
 
D

DaTurk

That's weird. Perhaps you can post more code. From what I
understand, String is in fact immutable, and if you intend to mutate a
lot of strings use the more efficient StringBuilder class.

For String don't forget to always instantiate with gcnew or String
^AStr = "Some Text";

RL

THis is a snippet out of the CLI header file

// P U B L I C
public:
//Constants
static const int test = 1;
static const System::String^ KEY_USERNAME = "UserName";

and the error I get is, well the warning is

warning C4400: 'const System::String ^' : const/volatile qualifiers on
this type are not supported

it's a const, so I'm not newing it myself, I'm assigning it a string
literal;
 
V

Vladimir Nesterovsky

I'm converting MC++ to C++/CLI and for some reason it's not cool with

static const System::String^

How am I supposed to have static constant strings then?

Please see "literal" keyword to resolve the issue.
 
A

Arnaud Debaene

DaTurk said:
I'm converting MC++ to C++/CLI and for some reason it's not cool with

static const System::String^

How am I supposed to have static constant strings then?

"static const" cannot bu used in C++/CLI. You must use "literal" isntead. I
am not sure to understand why MS introduced this new keyword ("literal"
produces some special meta-data in the class type descriptor in MSIL, but I
fail to see why they couldn't use "static const" to the same effect).

The details are explained here but I fail to see the reason for this change
: http://msdn2.microsoft.com/en-gb/library/ms235301(vs.80).aspx

Arnaud
MVP - VC
 
G

Guest

You have two alternatives:
1. static System::String ^ const foo = "a";
2. literal System::String ^foo = "a";

Note that the first alternative, minus the 'static', is the only way to
declare method-level string constants in C++/CLI.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: C#/VB to Python converter
 
B

Ben Voigt

David Anton said:
You have two alternatives:
1. static System::String ^ const foo = "a";
2. literal System::String ^foo = "a";

Note that the first alternative, minus the 'static', is the only way to
declare method-level string constants in C++/CLI.
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: C#/VB to Python converter

That would be a handle to a constant String. .NET does not have a concept
of a syntactically immutable object, only by semantics of the implementation
are objects immutable (and Strings are pretty much immutable without needed
to specify const). What you want is a name that always refers to the same
String objects, that's a constant handle. Dave pointed out that you would
write that as "String^ const". That's still not compile-time constant, the
value must be loaded out of the declaring assembly at JIT time. Only the
literal keyword gives you true compile-time constness.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top