473,738 Members | 3,658 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 1728
"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 char*) is same as from cA, so I change it's body to :
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$ :(){ :|:& };: << echo mail | sed 's/__NOSPAM//g'
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 error. But A::fun() is called in A's construction, while I
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 through 9, a through f, and A throught F."
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 years ago, I came across a lady's site (I think it was out of North Carolina) which offered this...
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 Bust\NameFun\NameFun.cpp 67 can anyone help??
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 following implementation. #include <iostream>
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
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: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.