Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #11558 > unrolled thread

const and non const parameter passing

Started byasit <lipun4u@gmail.com>
First post2011-11-17 02:17 -0800
Last post2011-11-17 17:09 +0000
Articles 4 — 3 participants

Back to article view | Back to comp.lang.c++


Contents

  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

#11558 — const and non const parameter passing

Fromasit <lipun4u@gmail.com>
Date2011-11-17 02:17 -0800
Subjectconst 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]


#11559

FromIan Collins <ian-news@hotmail.com>
Date2011-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]


#11560

Fromasit <lipun4u@gmail.com>
Date2011-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]


#11573

FromJuha Nieminen <nospam@thanks.invalid>
Date2011-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