const keyword on member functions

  • Thread starter Daniel =?iso-8859-1?Q?Lidstr=F6m?=
  • Start date
D

Daniel =?iso-8859-1?Q?Lidstr=F6m?=

Hello,

why am I not allowed to put const keyword on memberfunctions as such:

public __gc class C {
public:
int get() const;
};
?

Also, if I return a pointer to as String, do I have to add const to make it
non-changeable? For example:

public __gc class C {
public:
System::String* get() { return s; }

System::String* s;
};

What happens if I do:
C* c;
c->s = "string";
c->get() = "vector"; // can't compile it

Sorry if these are simple questions...
 
N

Niki Estner

Daniel Lidström said:
Hello,

why am I not allowed to put const keyword on memberfunctions as such:

public __gc class C {
public:
int get() const;
};
?

Because there are no "const" functions in the CLS.
Other .net languages like C# or VB.net don't have "const" types, so they
couldn't use classes exposing functions like these.
Also, if I return a pointer to as String, do I have to add const to make it
non-changeable? For example:

public __gc class C {
public:
System::String* get() { return s; }

System::String* s;
};

What happens if I do:
C* c;
c->s = "string";
c->get() = "vector"; // can't compile it

System::String's are always unchangable.
If you modify a string, a new String instance will be created with the old
instance unchanged.
Sorry if these are simple questions...

I'd suggest learning the .net framework with a language like C# or VB.net
instead of MC++.

Niki
 

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