Skip to main content
GameDev.net gamedev.net
🔒 Locked

Making this const

Started by c0nfUzEd Dec 28, 2001 at 3:55 AM 4 replies 350+ views
Original Post
c0nfUzEd
c0nfUzEd
  
class vector {
public:
   double x, y, z;
   double length ();
};

.
.
.

void dostuff (const vector f) {
   double l = f.length ();
}
  
I have some code like this, which the compiler refuses to compile, because f is supposed to be const. How do I tell the compiler that the "this" argument to vector::length () is a const?
_Hal_
_Hal_
c0nfUzEd

Do you need that f to be const? Why?


_________________________
Hal - Atena Developer
www.grupoatena.hpg.com.br



Edited by - _Hal_ on December 28, 2001 5:33:35 AM
c0nfUzEd
c0nfUzEd
It''s not absolutely necessary, but I dont change f in the function so it''s nice to be able to see that straight away.

Also I have inline functions that pass by reference, and I like to make them const if I can to help the compiler do its optimisations.
DrPizza
DrPizza
Change it to read
  double length () const;  

char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;} /*** drpizza@battleaxe.net ***/
c0nfUzEd
c0nfUzEd
Ah, so thats where the const goes.

Cool, thanks
Stoffel
Stoffel
FYI, your doStuff function is creating a copy of your vector. I would suggest passing-in the vector by reference:
void dostuff (const vector &f)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.