Thread: newbie variable question

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    133

    newbie variable question

    Hey guys, i was just wondering are constants still refered too as variables?

    I was also wondering if there such as think as const arrays and if they are commonly used?

    thanks.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    when necessary

    Kuphryn

  3. #3
    Registered User
    Join Date
    Apr 2006
    Location
    Singapore
    Posts
    2
    When variables are labeled const, they remain variables yes. Unchanging variables.
    Functions can be labeled const as well.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    22

    Question

    Quote Originally Posted by Jreak
    When variables are labeled const, they remain variables yes. Unchanging variables.
    Functions can be labeled const as well.
    But doesn't const in front of a function mean constructor?

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Const variables generally compile by replacing each call to the variable with it's value. Thus you cannot take the address of a const value.

    I'm not sure about const arrays and how they work.

    There is no such thing as a const function. However there is such a thing as a const meathod. A const meathod is a meathod that promises not to change any non-mutable value in it's member class.

    The key word const is also used extensively to mark function (and meathod) referenced paramitters as not changeing the referenced value.

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    A constructor takes the same name as it's parent class. If you name a member function myclass() in a class name "myclass", C++ will assume the aforementioned function is the constructor. Honestly, where did you get such notions?
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    If you name a constant function does that mean you can't change the parameters or does it mean it can't be overloaded/overridden or both?

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by bumfluff
    If you name a constant function does that mean you can't change the parameters or does it mean it can't be overloaded/overridden or both?
    Functions cannot be const. Methods can. A method is a function that is part of an object (and thus not really a function).

    If you mark a method const, that means that the method cannot change the object that is a method of. (Unless the parts it changes are explicitly marked mutable).

    It is also possible to mark parameters const. This applies to both methods and functions, but it is done on a single parameter basis. That is, each parameter must have its own indicator saying that it's const. Marking parameters const makes the function unable to change the arguements passed by those parameters.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about printing a variable???
    By Hoser83 in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2006, 01:57 PM
  2. Newbie Question - fflush(stdin) & fpurge(stdin) on Mac and PC
    By tvsinesperanto in forum C Programming
    Replies: 34
    Last Post: 03-11-2006, 12:13 PM
  3. Newbie question
    By dogbert234 in forum Windows Programming
    Replies: 1
    Last Post: 07-08-2005, 04:02 AM
  4. total newbie question
    By Unregistered in forum C++ Programming
    Replies: 15
    Last Post: 08-16-2002, 09:38 PM
  5. global variable and header question
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 08-05-2002, 11:38 PM