473,782 Members | 2,487 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 15360
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
4365
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
2619
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
1711
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
1725
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
2482
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
1858
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
3985
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
2217
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
9155
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
9639
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
10311
Oralloy
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10080
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8967
agi2029
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...
1
7492
isladogs
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...
0
6733
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
5378
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...
2
3639
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2874
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.