one doubt abt memberfunctions.....
-
tis a crap question but i still would want to clear em..... say that i have class say:
class xyz
{
int a;
public:
int return_value(void);
};int xyz::return_value(void)
{
return(a);
}as the above function is does not manipulate the data of the class,we can declare it also as ......
class xyz
{
int a;
public:
int return_value(void) const;
};int xyz::return_value(void) const
{
return(a);
}right?.....what i want to know iz iz there any other advantage of writing a 'const' after function that does not manipulate the data?..... cheerz.....:-D "faith, hope, love remain, these three.....; but the greatest of these is love" -1 Corinthians 13:13
-
tis a crap question but i still would want to clear em..... say that i have class say:
class xyz
{
int a;
public:
int return_value(void);
};int xyz::return_value(void)
{
return(a);
}as the above function is does not manipulate the data of the class,we can declare it also as ......
class xyz
{
int a;
public:
int return_value(void) const;
};int xyz::return_value(void) const
{
return(a);
}right?.....what i want to know iz iz there any other advantage of writing a 'const' after function that does not manipulate the data?..... cheerz.....:-D "faith, hope, love remain, these three.....; but the greatest of these is love" -1 Corinthians 13:13
AFAIK, the 'advantage' of using a constant function is to protect member variable integrity from being modified. Obviously you would not use it everywhere, but for areas of your class that has functions that you know should be read-only, then this sort of safe-guards the class from unscrupulously changing data it shouldn't.
I Dream of Absolute Zero
-
tis a crap question but i still would want to clear em..... say that i have class say:
class xyz
{
int a;
public:
int return_value(void);
};int xyz::return_value(void)
{
return(a);
}as the above function is does not manipulate the data of the class,we can declare it also as ......
class xyz
{
int a;
public:
int return_value(void) const;
};int xyz::return_value(void) const
{
return(a);
}right?.....what i want to know iz iz there any other advantage of writing a 'const' after function that does not manipulate the data?..... cheerz.....:-D "faith, hope, love remain, these three.....; but the greatest of these is love" -1 Corinthians 13:13
It simply indicates to the caller that the method is "read-only" and does not modify the object for which it is called. It can also serve as a reminder to anyone that changes the class.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
-
It simply indicates to the caller that the method is "read-only" and does not modify the object for which it is called. It can also serve as a reminder to anyone that changes the class.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
DavidCrow wrote: It simply indicates to the caller that the method is "read-only" and does not modify the object for which it is called. It can also serve as a reminder to anyone that changes the class. ...and prevent any modification on the object from within the function...
TOXCCT >>> GEII power
[toxcct][VisualCalc] -
DavidCrow wrote: It simply indicates to the caller that the method is "read-only" and does not modify the object for which it is called. It can also serve as a reminder to anyone that changes the class. ...and prevent any modification on the object from within the function...
TOXCCT >>> GEII power
[toxcct][VisualCalc]Yes, that's what "does not modify the object for which it is called" means.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
-
tis a crap question but i still would want to clear em..... say that i have class say:
class xyz
{
int a;
public:
int return_value(void);
};int xyz::return_value(void)
{
return(a);
}as the above function is does not manipulate the data of the class,we can declare it also as ......
class xyz
{
int a;
public:
int return_value(void) const;
};int xyz::return_value(void) const
{
return(a);
}right?.....what i want to know iz iz there any other advantage of writing a 'const' after function that does not manipulate the data?..... cheerz.....:-D "faith, hope, love remain, these three.....; but the greatest of these is love" -1 Corinthians 13:13
The compiler will complain if you try to modify data from within the function. Also, if you declare your member functions const, that'll enable callers to call them from const functions.. Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
The compiler will complain if you try to modify data from within the function. Also, if you declare your member functions const, that'll enable callers to call them from const functions.. Regards Senthil _____________________________ My Blog | My Articles | WinMacro
S. Senthil Kumar wrote: Also, if you declare your member functions const, that'll enable callers to call them from const functions. ...to call them from
const
objects. :) Peace! -=- James
If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Tip for new SUV drivers: Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
DeleteFXPFiles & CheckFavorites (Please rate this post!) -
DavidCrow wrote: It simply indicates to the caller that the method is "read-only" and does not modify the object for which it is called. It can also serve as a reminder to anyone that changes the class. ...and prevent any modification on the object from within the function...
TOXCCT >>> GEII power
[toxcct][VisualCalc]It doesn't "prevent" such modifications. Just makes it more difficult and explicit (like using const_cast). ----------------------------- Get trial copy of comment generating tool CommentMakerPro, a collection of convenience and productivity tools for Microsoft Visual Studio .NET FeinStuff, and std::string and std::string containers viewer FeinEvaluatorPro at www.FeinSoftware.com
-
S. Senthil Kumar wrote: Also, if you declare your member functions const, that'll enable callers to call them from const functions. ...to call them from
const
objects. :) Peace! -=- James
If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Tip for new SUV drivers: Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
DeleteFXPFiles & CheckFavorites (Please rate this post!)That's right :) Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
It doesn't "prevent" such modifications. Just makes it more difficult and explicit (like using const_cast). ----------------------------- Get trial copy of comment generating tool CommentMakerPro, a collection of convenience and productivity tools for Microsoft Visual Studio .NET FeinStuff, and std::string and std::string containers viewer FeinEvaluatorPro at www.FeinSoftware.com
I would REALLY appreciate if whoever voted my post down (not that I care about the "score") explained their position. Vlad. ----------------------------- Get trial copy of comment generating tool CommentMakerPro, a collection of convenience and productivity tools for Microsoft Visual Studio .NET FeinStuff, and std::string and std::string containers viewer FeinEvaluatorPro at www.FeinSoftware.com