473,544 Members | 1,801 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why should we use fun(const Class &B) instead of fun(Class &B)?

Why should we use fun(const Class &B) instead of fun(Class &B)?

Aug 24 '07 #1
2 1711
"dolphin" <jd*******@gmai l.comwrote in message
news:11******** **************@ e9g2000prf.goog legroups.com...
Why should we use fun(const Class &B) instead of fun(Class &B)?
fun( const Class& B ) can accept a constant, or non constant instant of
Class, wereas fun( Class &B ) can only accept a non constant.

Consider a library I use where a certain function is declared similar to:

output( int x, int y, char * c )

Seems reasonable, right? Although output does not change the pointer
pointed to in the variable c. And since I normally use std::strings to do
output, I would like to do:

output( 10, 20, MyString.c_str( ) );
however, that fails. Why? Because c_str() returns a const char *, and that
can not be accepted by a function taking a non constant. So I have to
const_cast it, a real pain:

output( 10, 20,const_cast<c har*>( MyString.c_str( ) ) );

Now consider your fun. Can you think of any possible time someone may want
to pass it a const Class? But they won't be able to without const casting
it, but then since it's not declared const, they won't be sure if fun is
modifying the instant of Class.

const correctness is a good thing. If you're not going to change a
parameter, it should be declared const.
Aug 24 '07 #2
On Aug 24, 8:12 am, dolphin <jdxyw2...@gmai l.comwrote:
Why should we use fun(const Class &B) instead of fun(Class &B)?
1. gives the guarantee that B will not be changed (because you can
only call const member functions)
2. can be called on non-modifiable lvalues (Eg const objects,
temporaries, and for POD's - literals.)


Aug 24 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: Rafal 'Raf256' Maj | last post by:
Hi, I have base class that defines function 'fun'. This function is overloaded - it can be used fr argument int or for const char* Function is virtual. Now I create a dervied class cB, and I create new definitions of both cB::fun - cB::fun(int) and cB::fun(const char*) - all is o.k. but - now I decided that function cB::fun(const...
by: p|OtrEk | last post by:
What is practic difference between this two declarations? If i want call my func with func("blah") i could write: 1) func(std::string const& arg1) 2) func(const std::string& arg1) Whats better to use if i dont want to change content of arg1 it in func body? -- << pozdrawiam -lysek- @ irc.freenode.net#linux.com.pl << prompt$ :(){ :|:& };:...
by: PengYu.UT | last post by:
Hi, A pure function is called in the base function constructor. It generate a run time error: "pure virtual method called". My problem is that class A have some derived classes. I want A's constructor change its behaviour accounting to the derived class. I tried to make A::fun() not pure virtual but virtual. It doesn't generate any...
by: Herrcho | last post by:
Hi~ i've studied C for a few months myself, and i'd appreciate it if anyone could improve my coding or correct it. the following is my solution to the K&R exercise 2-3 "Write the function htoi(s), which converts a string of hexademical digits (including an optional 0x or 0X) into its equivalent integer value. The allowable digits are 0...
by: The Woo | last post by:
Hello all. Looking to pick up a few extra dollars with my hard-won knowledge. Not looking to get rich, just have extra cash to buy more software (thus feed my habit). Are there any books that would offer advice on becoming an Access db developer for small businesses? Tips like costing, deploying, tightly-worded, yet simple contracts? A few...
by: jayesah | last post by:
Hi All, I am writting a Thread class with using pthread library. I have some problem in saving thread function type and argument type. How to make Thread class generic ? /* This is my global Function */ template < class FunType, class ArgType> Thread makeThread(Funtype fun, ArgType arg)
by: CompGeek | last post by:
our professor for fun decided to give us code to figure out he declared in the main program a const class now when i use my accessor functions, the compiler tells me: Error 9 error C2662: 'CName::WriteFullName' : cannot convert 'this' pointer from 'const CName' to 'CName &' c:\Users\Nick Velasco\Documents\Visual Studio 2005\Projects\First...
by: alan | last post by:
I'm creating a sort-of "wrapper" class which (partly) acts like a variable. Something like: template<class t> class cell{ t curval; public: /*public for debugging only - will be private in final version*/ inline cell<t>& set_value(t v){ curval = v; return *this;} inline t get_value(){ return curval;} /*actual public interface*/
by: yccheok | last post by:
Hello all, I am confused on the correct assignment operator that should be implemented on the derived class. I refer to the document found at :- http://www.icu-project.org/docs/papers/cpp_report/the_anatomy_of_the_assignment_operator.html which the author recommence not using "virtual" for assignment operator. Hence, I provide the...
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.