Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #11558 > unrolled thread
| Started by | asit <lipun4u@gmail.com> |
|---|---|
| First post | 2011-11-17 02:17 -0800 |
| Last post | 2011-11-17 17:09 +0000 |
| Articles | 4 — 3 participants |
Back to article view | Back to comp.lang.c++
const and non const parameter passing asit <lipun4u@gmail.com> - 2011-11-17 02:17 -0800
Re: const and non const parameter passing Ian Collins <ian-news@hotmail.com> - 2011-11-17 23:24 +1300
Re: const and non const parameter passing asit <lipun4u@gmail.com> - 2011-11-17 02:20 -0800
Re: const and non const parameter passing Juha Nieminen <nospam@thanks.invalid> - 2011-11-17 17:09 +0000
| From | asit <lipun4u@gmail.com> |
|---|---|
| Date | 2011-11-17 02:17 -0800 |
| Subject | const and non const parameter passing |
| Message-ID | <33539597.678.1321525054013.JavaMail.geo-discussion-forums@prou19> |
why the following code does not compile at line 14
#include <iostream>
#include <cstdlib>
using namespace std;
struct One
{
void funct1()
{
cout<<"Testing..."<<endl;
}
};
void funct2( const One& c) //line 14
{
c.funct1();
}
int main()
{
One b;
funct2(b);
return 0;
}
[toc] | [next] | [standalone]
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Date | 2011-11-17 23:24 +1300 |
| Message-ID | <9ik5nbFsoiU1@mid.individual.net> |
| In reply to | #11558 |
On 11/17/11 11:17 PM, asit wrote:
> why the following code does not compile at line 14
>
> #include<iostream>
> #include<cstdlib>
>
> using namespace std;
>
> struct One
> {
> void funct1()
> {
> cout<<"Testing..."<<endl;
> }
> };
>
> void funct2( const One& c) //line 14
> {
> c.funct1();
It will fail here. funct1() is a non-const member function, c is a const
object.
--
Ian Collins
[toc] | [prev] | [next] | [standalone]
| From | asit <lipun4u@gmail.com> |
|---|---|
| Date | 2011-11-17 02:20 -0800 |
| Message-ID | <6728103.679.1321525208615.JavaMail.geo-discussion-forums@prou19> |
| In reply to | #11558 |
sorry...the error is at line 16
[toc] | [prev] | [next] | [standalone]
| From | Juha Nieminen <nospam@thanks.invalid> |
|---|---|
| Date | 2011-11-17 17:09 +0000 |
| Message-ID | <4ec53faf$0$3082$7b1e8fa0@news.nbl.fi> |
| In reply to | #11558 |
asit <lipun4u@gmail.com> wrote:
> using namespace std;
You wrote 20 characters (and a couple of newlines) in order
to save writing 5 characters (namely "std::") twice. Overall the
size of your program increased by 12 characters and two lines
without any obvious benefit (such as the program becoming easier
to read and understand). Was it really worth it?
> struct One
> {
> void funct1()
> {
> cout<<"Testing..."<<endl;
> }
> };
>
> void funct2( const One& c) //line 14
> {
> c.funct1();
> }
funct1() must be const if you want to call it using a const
object.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c++
csiph-web