473,386 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

const keyword on member functions

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...

--
Daniel
Jul 21 '05 #1
1 1279
"Daniel Lidström" <so*****@microsoft.com> wrote in
news:av***************************@40tude.net...
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
Jul 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jim West | last post by:
Could someone please explain to me why the code segment class FOO { public: double *begin(); }; void bar(const FOO &foo) { foo.begin(); }
20
by: Corno | last post by:
Hi all, There's probably a good reason why a const object can call non const functions of the objects where it's member pointers point to. I just don't see it. For me, that makes the the const...
1
by: Daniel Lidström | last post by:
Hello, why am I not allowed to put const keyword on memberfunctions as such: public __gc class C { public: int get() const; }; ?
20
by: Snis Pilbor | last post by:
Whats the point of making functions which take arguments of a form like "const char *x"? It appears that this has no effect on the function actually working and doing its job, ie, if the function...
15
by: Jiří PaleÄek | last post by:
Hello, I know the rules for const handling in C++, but I'd like to ask what is the "right" way to use them, eg. when is it appropriate to make a member function const? This came across this...
4
by: grizggg | last post by:
I have searched and not found an answer to this question. I ran upon the following statement in a *.cpp file in a member function: static const char * const pacz_HTMLContentTypeHeader =...
23
by: Kira Yamato | last post by:
It is erroneous to think that const objects will have constant behaviors too. Consider the following snip of code: class Person { public: Person(); string get_name() const
2
by: Angus | last post by:
I have a member function, int GetLogLevel() which I thought I should change to int GetLogLevel() const - I made the change and it works fine. But in the function I am creating buffers and of...
3
by: kk_oop | last post by:
Hi. I just wrote a function that returns a const pointer: MyClass { .... public: virtual ReturnClass * const getReturnClass( ); .... }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.