cosnt inside parameter of function

October 25, 2010 at 23:49:09
Specs: Windows XP
can anybody explain about the use of const before void and inside the parameter and here & sign with other is this show an alias of the variable with whome it will be called

const void operator=(const point& other);

please explain it
thanks


See More: cosnt inside parameter of function

Report •


#1
November 14, 2010 at 10:28:37
const inside function specifies that the value of parameter will not change inside that function.

for example ........

void foo(const int num)
{
num = 5; // this will not compile because i'm changing the value of num
}

...................................................................................................................
_Adnan_


Report •

#2
November 14, 2010 at 17:34:13
_Adnan_ - - - you forgot the const in front of the function???????????
what doest mean,
i think you can not write const in front of the function, but you can do this :

void any_func() const
{
-------
}
it means this function can't change (the value) of an object calls it.
but i am not sure so ask somebody to make sure


Report •

#3
November 14, 2010 at 19:09:57
it means this function can't change (the value) of an object calls it.

you are right.

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

void foo(int numOne, const int numTwo)
{
numOne = 6; // we can change numOne's value
numTwo = 7; // we can't change numTwo's value
}

void soo() const
{
numOne = 6; // it will not compile because we can't change numOne's value
numTwo = 7; // it will not compile because we can't change numTwo's value
}

void hoo() const
{
cout << numOne; // it will compile because we aren't changing numOne's value. we are just using numOne's value
cout << numTwo; // it will compile because we aren't changing numTwo's value. we are just using numTwo's value
}

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

_Adnan_


Report •

Related Solutions

#4
November 14, 2010 at 19:33:17
const char* RainString();

This finction will return const string that we can't change.

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

#include <iostream.h>

const char* RainString();

int main()
{
int i;
char* str; // Notice str is char*
str = RainString(); // this is not compiling because RainString() returns const char* not char*
cout << str ;
cin >> i;
}

const char* RainString()
{
return "Rian";
}

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

#include <iostream.h>

const char* RainString();

int main()
{
int i;
const char* str; // Now str is const char*
str = RainString(); // Now this will compile
cout << str ;
cin >> i;
}

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

const char* RainString()
{
return "Rian";
}

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

Do you need more info?

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

_Adnan_


Report •

#5
November 16, 2010 at 14:31:00
Ohhh thats good and useful thank you, i knew something about it, but now its more clear, specially with the cons char* return

Report •


Ask Question