473,695 Members | 1,560 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

const type& and type const&

What is practic difference between this two declarations?
If i want call my func with func("blah") i could write:
1) func(std::strin g 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.ne t#linux.com.pl
<< prompt$ :(){ :|:& };:
<< echo mail | sed 's/__NOSPAM//g'

Jul 23 '05 #1
6 15326
p|OtrEk wrote:
What is practic difference between this two declarations?
If i want call my func with func("blah") i could write:
1) func(std::strin g 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?


They're the same.

The types are read from right to left.

consider:

const int i; // i is a const int
int const i; // i is a const int
const int * i // i is a pointer to a const int.
int const * i // i is a pointer to a const int
int * const i // i is a const pointer to an int
const int * const i // i is a const pointer to a const int
int const * const i // i is a const pointer to a const int

References cannot be reseated (c.f., pointers), so they are already "const".

It's usually easier to read:
const int i;

yet, when stacking them up, it's usually easier the other way around:
int const * const

Ben
--
I'm not just a number. To many, I'm known as a String...
Jul 23 '05 #2
Ben Pope wrote:
They're the same.
(...)
References cannot be reseated (c.f., pointers), so they are already

"const"
Thank you.
--
<< pozdrawiam -lysek- @ irc.freenode.ne t#linux.com.pl
<< prompt$ :(){ :|:& };:
Jul 23 '05 #3
On more thing,
is this legal to write:
const int& fun () { return 1; } // so it returns reference to const '1'
??
--
<< pozdrawiam -lysek- @ irc.freenode.ne t#linux.com.pl
<< prompt$ :(){ :|:& };:
Jul 23 '05 #4
lysek wrote:
On more thing,
is this legal to write:
const int& fun () { return 1; } // so it returns reference to const '1'


You'd be returning a reference to a local... it would have lost it's scope by the time the function returned. This is undefined behavious, as far as I know.

It's usually best/safest to return by value.

Ben
--
I'm not just a number. To many, I'm known as a String...
Jul 23 '05 #5
Ben Pope wrote:
lysek wrote:
On more thing, is this legal to write:
const int& fun () { return 1; } // so it returns reference to const '1'


You'd be returning a reference to a local... it would have lost it's scope by the time the function returned. This is undefined behavious, as far as I know.


1 is an integer literal, not a local variable.

Jul 23 '05 #6
Mercator wrote:
Ben Pope wrote:
lysek wrote:
On more thing, is this legal to write:
const int& fun () { return 1; } // so it returns reference to const '1'


You'd be returning a reference to a local... it would have lost it's scope by the time the function returned. This is undefined behavious, as far as I know.

1 is an integer literal, not a local variable.


Yeah, I guessed somebody would mention that when I hit send. I was kind of extrapolating his use from the previous example, I guess this is why posting ACTUAL code rather than
noddy examples that are supposed to be equivalent, but aren't, is never a good idea.

Ben
--
I'm not just a number. To many, I'm known as a String...
Jul 23 '05 #7

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

Similar topics

6
4362
by: hoox2 | last post by:
void push_front(Node const*& head, int data); Can someone tell me what it means for "const*&"? A reference or a pointer?
12
2609
by: zealotcat | last post by:
template <class T> inline T const& max (T const& a, T const& b) { // if a < b then use b else use a return a<b?b:a; } thanks very much!!
0
1700
by: tom olson | last post by:
After more searching I found that defining const operators can cause problems with many compilers due to the way it interprets the C++ standard. I removed the const operators from my class and it seems to be working fine now. >-----Original Message----- >I have C++ code that has been running just fine in VS6 for >a couple of years. I brought it into VS.NET with managed
1
1721
by: Kip | last post by:
Greetings, Could anyone explain to me how the parameter type works here? I am also interested in the technical details of what is actually happening on the stack. I have never seen a "char * const &szFoo". I usually just use "char *pszFoo" whenever I want to pass a string, or maybe "const char *pszFoo" if I know that I won't modify it. Thanks. // Hash a string... unsigned int StringHash(char * const &szFoo)
8
2478
by: vsgdp | last post by:
Does this mean (T* const)& (i.e., a reference to a constant pointer to a T). So the pointer is constant but not the object pointed to?
9
1851
by: miaohua1982 | last post by:
the program is as follows: #include <vector> using namespace std; class A{}; int main() { A* const &p = NULL; vector<A*B(3,NULL); //there is a compile error B.push_back(NULL);
13
3974
by: dragoncoder | last post by:
Hi everyone, please consider the following function:- const int& foo ( const double& d ) { return d; } g++ compiles it with warnings and solaris CC gives error. I want to know if the code is correct according to the standard ?
2
2211
by: ek | last post by:
This first example does not work (cannot be overloaded): int& operator()(int a) { // (1) return a; } int const& operator()(int a) { // (2) return a; }
2
9152
by: nassim.bouayad.agha | last post by:
Hello, here is a code snippet showning my problem : template<typename _K> class TClass1 { public: void Process(const _K& arg) const {
0
8623
marktang
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...
0
8565
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,...
0
8977
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8822
tracyyun
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...
0
5837
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();...
0
4339
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...
1
2997
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
2
2269
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1971
bsmnconsultancy
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.