MudBytes
» MUDBytes Community » Language Discussions » C and C++ » a const question
Pages: << prev 1, 2 next >>
a const question
JohnnyStarr
Wizard






Group: Members
Posts: 953
Joined: Feb 14, 2009

Go to the bottom of the page Go to the top of the page
#1 id:41907 Posted Feb 10, 2010, 10:50 pm

I'm curious why C++ requires the programmer to define everything that is const.
I understand char pointers, and a few other things, but instance methods? My true question is how Java / C# has managed
to keep so much of the C++ syntax, yet it seems that this isn't as much (if at all) of an issue. I am not questioning the logic
of protecting the application from bugs, I'm just wondering if it is more of a compiler matter.

Also, I'll throw this in here too. having to write:
Code (C++):
1
2
3
4
5
6
7
8
9
10
11
 
std::list<std::string&>::iterator str;
 
// I guess you could do this
using namespace std;
 
list<string&>::iterator str;
// but it's still a lot
 

Is so much just to get an iterator object. Is this the only way to do this? I suppose typedef could take care of most of the labor.
I'm afraid I just don't quite get all of C++'s quirks. Maybe that's why I'm all about learning it though!

Last edited Feb 10, 2010, 10:53 pm by JohnnyStarr
Runter
Wizard






Group: Developer
Posts: 2,929
Joined: Jun 1, 2006

Go to the bottom of the page Go to the top of the page
#2 id:41909 Posted Feb 10, 2010, 10:58 pm

Eh, "so much" is objective here.  An iterator is a completely different typed object (albeit template in your example) from the haystack in C++.  It's not a language construct.  It just so happens that someone thought the semantics you think is too much effort were appropriate.    I agree with them.

Also, I'm not sure why you think const isn't implicit enough.  How about an example of where you don't like having to specify const?
.........................
CoralMud project (Mud codebase in Ruby)
HaikuMud project (HTML/JS/Ruby, web-app based MMO codebase)
My Web CV

Last edited Feb 10, 2010, 10:58 pm by Runter
JohnnyStarr
Wizard






Group: Members
Posts: 953
Joined: Feb 14, 2009

Go to the bottom of the page Go to the top of the page
#3 id:41913 Posted Feb 10, 2010, 11:24 pm

Code (C++):
1
2
3
4
5
 
const char*const instanceMethod(const char*const&) const;
 


This is contrived to make a point, but it could come up if needed.
I guess I'm spoiled with Ruby.

Last edited Feb 10, 2010, 11:24 pm by JohnnyStarr
David Haley
Wizard






Group: Members
Posts: 7,783
Joined: Jun 30, 2007

Go to the bottom of the page Go to the top of the page
#4 id:41916 Posted Feb 11, 2010, 1:08 am

But in that case, each const actually means something. Presumably they were added for a reason.

Instance methods being const are also quite important. Consider:

Code (text):
1
2
3
4
const MyObject* a;
a->changeYourself()


where we assume that "changeYourself" is a method that is non-const.

Since a is const, isn't it a violation to try to do something to it that changes it? The whole point of constness is to avoid that. Therefore instance methods that are "const safe" (i.e., don't modify the object) mark themselves as such so that the compiler knows that (a) it's safe to call them on const objects, and (b) it should prevent any modification from occurring from within the method definition.

To be honest I actually really miss constness from time to time in other languages. It's a very nice way of expressing the intention of what you're trying to do. E.g., this method is "read only", or this value is a constant, etc.

And yes, constness is entirely a compiler matter. (Well, I include the symbol resolution process in linkers as part of "the compiler".)


Regarding type verbosity, the next C++ standard will have the "auto" construct that makes a lot of this "type noise" go away. See for example the Wikipedia article on C++0x.

So you could do:

Code (text):
1
2
3
4
std::list<std::string> myList;
auto listItr = myList.begin();


Of course, C++ will probably always be a more syntax-heavy language than others.
.........................
-- d.c.h --
BabbleMUD Project (custom codebase)
Legends of the Darkstone (head coder)
http://david.the-haleys.org
.........................

Tyche
Wizard






Group: Members
Posts: 1,702
Joined: May 23, 2006

Go to the bottom of the page Go to the top of the page
#5 id:41922 Posted Feb 11, 2010, 5:13 am

I tend to "type" the minimum necessary "typing" in order to get the compiler to just STFU.
C++ is what it is.
.........................
Proud member of Team Hetero
http://jlsysinc.gotdns.com/ladybug_laugh2.jpghttp://jlsysinc.gotdns.com/teensymud_250x80.pnghttp://jlsysinc.gotdns.com/palin_calendar.jpg
For now we see through a glass, darkly; but then face to face: now I know in part; but then shall I know even as also I am known.

elanthis
Wizard




Group: Members
Posts: 772
Joined: Feb 26, 2008

Go to the bottom of the page Go to the top of the page
#6 id:41928 Posted Feb 11, 2010, 8:16 am


JohnnyStarr said:
I'm curious why C++ requires the programmer to define everything that is const.


Because otherwise the compiler doesn't know what is const.  Java is the same.

Quote:
I understand char pointers, and a few other things, but instance methods?


What?  Are you asking about virtual functions?

Quote:
My true question is how Java / C# has managed
to keep so much of the C++ syntax, yet it seems that this isn't as much (if at all) of an issue.


Java and C# are managed/interpreted languages, while C++ is designed to run bare to the metal.  C++ is made to be useful as a systems programming language, and no feature in C++ forces additional runtime overhead or complexity that is unnecessary (except exceptions, which are often turned off completely in many C++ domains, including kernels and games).

C++ requires you to be more explicit because you're essentially just using C with a small bit of syntactic sugar on sugar.  You can translate C++ into C relatively easily (assuming you have a full C++ parser already written, which is not at all easy to get).  Anything you can write in C++ you can write in C without too much extra effort, it'll just be even more verbose and even uglier and even harder to read and you'll have to do a lot of casting and cut-n-pasting (especially when trying to get the performance of templated code without templates).

Quote:
Is so much just to get an iterator object. Is this the only way to do this? I suppose typedef could take care of most of the labor.


Yup.  C++0x I believe adds some convenience methods to make this easier.  You could write your own as well using templates.

Quote:
I'm afraid I just don't quite get all of C++'s quirks. Maybe that's why I'm all about learning it though!


I highly suggest you pick up the Design and Evolution of C++.  It goes over the features of C++ and explains the purpose and reasoning behind each of them.  Might help clear up a lot of questions you have.
.........................
Cutting corners to keep your line count down is just sad.

Runter
Wizard






Group: Developer
Posts: 2,929
Joined: Jun 1, 2006

Go to the bottom of the page Go to the top of the page
#7 id:41982 Posted Feb 12, 2010, 1:40 am


JohnnyStarr said:
Code (C++):
1
2
3
4
5
 
const char*const instanceMethod(const char*const&) const;
 


This is contrived to make a point, but it could come up if needed.
I guess I'm spoiled with Ruby.



Yeah, but my point is those actually mean something.   

And you shoulda made it const char const * instanceMethod for good measure. :p
.........................
CoralMud project (Mud codebase in Ruby)
HaikuMud project (HTML/JS/Ruby, web-app based MMO codebase)
My Web CV

JohnnyStarr
Wizard






Group: Members
Posts: 953
Joined: Feb 14, 2009

Go to the bottom of the page Go to the top of the page
#8 id:45150 Posted Apr 13, 2010, 11:00 am

Here's a quick question.

In the following function, I want to change the second parameter from (char*) to (const char*)
Code (c):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 
bool is_name ( char *str, /*char * namelist*/ const char* namelist )
{
    char name[MAX_INPUT_LENGTH], part[MAX_INPUT_LENGTH];
    char *list, *string;
 
    /* fix crash on NULL namelist */
    if (namelist == NULL || namelist[0] == '\0')
    	return FALSE;
 
    /* fixed to prevent is_name on "" returning TRUE */
    if (str[0] == '\0')
	return FALSE;
 
    string = str;
    /* we need ALL parts of string to match part of namelist */
    for ( ; ; )  /* start parsing string */
    {
	str = one_argument(str,part);
 
	if (part[0] == '\0' )
	    return TRUE;
// ERROR:
	list = namelist;  
	for ( ; ; )  /* start parsing namelist */
	{
	    list = one_argument(list,name);
	    if (name[0] == '\0')  /* this name was not found */
		return FALSE;
 
	    if (!str_prefix(string,name))
		return TRUE; /* full pattern match */
 
	    if (!str_prefix(part,name))
		break;
	}
    }
}


We wont be able to asign the pointer if it is const. How do you make not the pointer const, but what it points to const?
I need the parameter to be const so that i can send std::string.c_str() in some cases.

Runter
Wizard






Group: Developer
Posts: 2,929
Joined: Jun 1, 2006

Go to the bottom of the page Go to the top of the page
#9 id:45152 Posted Apr 13, 2010, 11:02 am


char * const roar;
.........................
CoralMud project (Mud codebase in Ruby)
HaikuMud project (HTML/JS/Ruby, web-app based MMO codebase)
My Web CV

David Haley
Wizard






Group: Members
Posts: 7,783
Joined: Jun 30, 2007

Go to the bottom of the page Go to the top of the page
#10 id:45154 Posted Apr 13, 2010, 11:02 am

You just need list to be declared as a const char*, not a char*.

You can assign const char* to other const char* variables; a const char* means you can't change the contents.
.........................
-- d.c.h --
BabbleMUD Project (custom codebase)
Legends of the Darkstone (head coder)
http://david.the-haleys.org
.........................

JohnnyStarr
Wizard






Group: Members
Posts: 953
Joined: Feb 14, 2009

Go to the bottom of the page Go to the top of the page
#11 id:45165 Posted Apr 13, 2010, 7:51 pm

Quick question about instance members that are containers:

Because the STL provides encapsulated containers, is it conventional to use the provided public methods
alone to add / remove / sort data? Example:
Code (c++):
1
2
3
ch->quests.push_back(q); /*vs*/ ch->addQuest(q); // therefor keeping (quests) private

I suppose it depends on whether or not you need to enforce constraints as to how elements are added or deleted.
Just wondering what your views were on it. One of my c++ books states:
You should make every member private unless it can be proven that it needs to be public.

Last edited Apr 13, 2010, 7:52 pm by JohnnyStarr
Davion
Idle Hand






Group: Administrators
Posts: 1,537
Joined: May 14, 2006

Go to the bottom of the page Go to the top of the page
#12 id:45167 Posted Apr 13, 2010, 9:07 pm


JohnnyStarr said:
Quick question about instance members that are containers:

Because the STL provides encapsulated containers, is it conventional to use the provided public methods
alone to add / remove / sort data? Example:
Code (c++):
1
2
3
ch->quests.push_back(q); /*vs*/ ch->addQuest(q); // therefor keeping (quests) private

I suppose it depends on whether or not you need to enforce constraints as to how elements are added or deleted.
Just wondering what your views were on it. One of my c++ books states:
You should make every member private unless it can be proven that it needs to be public.


ch->addQuest(q) definitely looks a lot cleaner! Also, it's probably better to do it this way, in case (as you said) you need to put constraints or even event triggers on items adding to a list.
.........................
http://mudbytes.net/mudbytessignature-davion2.png

David Haley
Wizard






Group: Members
Posts: 7,783
Joined: Jun 30, 2007

Go to the bottom of the page Go to the top of the page
#13 id:45208 Posted Apr 14, 2010, 12:13 pm

You definitely do not want to add it to the list directly. Why? That forces your implementation to be a list. Everybody has to know about your implementation. What if you later decide that internally you want something else? (For example, a mapping from quest status to quest object. Whatever...) Now you no longer have this list to act upon, and you need to rewrite a pile of code.

This is almost exactly the use case for getters and setters (or in this case, "adders") rather than pushing variables to the public scope. :smile:
.........................
-- d.c.h --
BabbleMUD Project (custom codebase)
Legends of the Darkstone (head coder)
http://david.the-haleys.org
.........................

flumpy
Wizard






Group: Members
Posts: 623
Joined: Mar 27, 2009

Go to the bottom of the page Go to the top of the page
#14 id:45210 Posted Apr 14, 2010, 12:31 pm

David Haley said:
You definitely do not want to add it to the list directly. Why? That forces your implementation to be a list. Everybody has to know about your implementation. What if you later decide that internally you want something else? (For example, a mapping from quest status to quest object. Whatever...) Now you no longer have this list to act upon, and you need to rewrite a pile of code.

This is almost exactly the use case for getters and setters (or in this case, "adders") rather than pushing variables to the public scope. :smile:


You're right.

Internally to a particular API implementation, it might be perfectly feasible to make the variable package/namespace protected (if you can do that in C++, I forget) and have your package classes reference it directly until you decide it's time to expose it publicly when the time's right.

Saying that you should "Always make member variables private in all circumstances" is wrong (I think that was the quote above). You certainly should not always make them "public", but thats not the same thing.

.........................

GroovyMud - a mud engine written in Java and Groovy

try it out! telnet://zeno.biyg.org:2223


Disclaimer: this project is in alpha, and therefore in heavy development.

Check out GWABS, the desktop battler: from an idea I had on Cambrian House

Last edited Apr 14, 2010, 12:32 pm by flumpy
David Haley
Wizard






Group: Members
Posts: 7,783
Joined: Jun 30, 2007

Go to the bottom of the page Go to the top of the page
#15 id:45212 Posted Apr 14, 2010, 12:35 pm

Quote:
Saying that you should "Always make member variables private in all circumstances" is wrong (I think that was the quote above). You certainly should not always make them "public", but thats not the same thing.

No, that wasn't the quote, this is why copying and pasting is useful. :tongue:

"You should make every member private unless it can be proven that it needs to be public."
(Emphasis mine.)

The point of that quote is that by default you should think "hide the variable", and only show it if you really need to (and have a clear use case).
.........................
-- d.c.h --
BabbleMUD Project (custom codebase)
Legends of the Darkstone (head coder)
http://david.the-haleys.org
.........................

Pages:<< prev 1, 2 next >>

Valid XHTML 1.1! Valid CSS!