delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/07/11/09:21:18

From: "Steve Ball" <steve AT effectivejava DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: const class& as a parameter.
Date: Mon, 12 Jul 1999 00:38:39 +1200
Organization: Customer of Telecom Internet Services
Lines: 49
Message-ID: <7ma3bd$4rksn$1@titan.xtra.co.nz>
References: <3787A70E DOT 190417A AT unb DOT ca>
NNTP-Posting-Host: 210-55-144-157.dialup.xtra.co.nz
X-Trace: titan.xtra.co.nz 931696813 5100439 210.55.144.157 (11 Jul 1999 12:40:13 GMT)
X-Complaints-To: abuse AT xtra DOT co DOT nz
NNTP-Posting-Date: 11 Jul 1999 12:40:13 GMT
X-Newsreader: Microsoft Outlook Express 4.72.3110.1
X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

>Does this mean that I can't call any member functions of a class which is
>passed as a const-reference without risking modification of the class data?

No, that's fine, what you have. You just need to mark the member function to
show that it is one that doesn't attempt to modify the object:

class foo {
public:
  void member() const { your_code_here(); }
}

The const on the end of the member function means that it can be invoked for
const objects:

foo const f;
f.member();

or

bar(f);

Endlisnis wrote in message <3787A70E DOT 190417A AT unb DOT ca>...
>    I have some class "foo":
>class foo {...};
>
>And I have some function bar:
>void bar(const foo& foo2)
>{
> foo2.member();
>}
>
>    This generates a "warning: passing `const foo' as `this' argument of
`void
>foo::member()' discards const".
>Does this mean that I can't call any member functions of a class which is
>passed as a const-reference without risking modification of the class data?
I
>could just remove the 'const', but I did it to prevent other warning
messages
>like if I were to call "bar((foo)5)".
>--
>     (\/) Endlisnis (\/)
>          s257m AT unb DOT ca
>          Endlisnis AT HotMail DOT com
>          ICQ: 32959047
>
>


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019