C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

const functions and IO

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Mike P. Wagner
Guest





PostPosted: Fri Jun 27, 2003 7:13 pm    Post subject: const functions and IO Reply with quote



I have a real world problem that I hope someone has solved. I
implementing a class in a kernel that stores (and reads) data
persistently. There is one underlying "raw IO" function that calls the
device. This function cannot be const (unless I lie) since it changes
persistent data.

But that means that none of my read functions can be const, since they
need to call the underlying "raw IO" function. Now that seems really
silly.

Is there a simple way to tell the compiler, "Look, when I call this
function with read requests, it's const"?

I know that I can goof around with typedefs and reinterpret_cast<> to
do this, but it seems like there must be a more elegant way to do
this.

It's not an exotic problem, is it?

Mike
Back to top
John Harrison
Guest





PostPosted: Fri Jun 27, 2003 7:28 pm    Post subject: Re: const functions and IO Reply with quote




"Mike P. Wagner" <mikepwagner (AT) hotmail (DOT) com> wrote

Quote:
I have a real world problem that I hope someone has solved. I
implementing a class in a kernel that stores (and reads) data
persistently. There is one underlying "raw IO" function that calls the
device. This function cannot be const (unless I lie) since it changes
persistent data.

But that means that none of my read functions can be const, since they
need to call the underlying "raw IO" function. Now that seems really
silly.

Is there a simple way to tell the compiler, "Look, when I call this
function with read requests, it's const"?

I know that I can goof around with typedefs and reinterpret_cast<> to
do this, but it seems like there must be a more elegant way to do
this.

It's not an exotic problem, is it?

Mike

Declare your persistent data to be mutable and declare the rawIO function
const. Even const functions are allowed to change mutable data

class X
{
mutable int data;
};

mutable is useful on caches and the like, which if I understand you
correctly is what you have.

john



Back to top
Thomas Matthews
Guest





PostPosted: Fri Jun 27, 2003 7:28 pm    Post subject: Re: const functions and IO Reply with quote



Mike P. Wagner wrote:
Quote:
I have a real world problem that I hope someone has solved. I
implementing a class in a kernel that stores (and reads) data
persistently. There is one underlying "raw IO" function that calls the
device. This function cannot be const (unless I lie) since it changes
persistent data.

But that means that none of my read functions can be const, since they
need to call the underlying "raw IO" function. Now that seems really
silly.

Is there a simple way to tell the compiler, "Look, when I call this
function with read requests, it's const"?

I know that I can goof around with typedefs and reinterpret_cast<> to
do this, but it seems like there must be a more elegant way to do
this.

It's not an exotic problem, is it?

Mike

Most of the time, reading is not const and destroys things.
If you want to make member function const and have it modify
member variables, declare those variables as "mutable".

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.