473,378 Members | 1,420 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

mutable base class

I'm using a private base class as part of the implementation for one of
my classes. In my implementation, it holds cached data, and I'd like to
be able to call its const member functions from my nonconst members. Is
there a way to get something like "mutable inheritance" to accomplish this?
Jul 22 '05 #1
5 1304
On Fri, 02 Apr 2004 14:33:34 -0700, "Adam H. Peterson" <ah**@email.byu.edu>
wrote:
I'm using a private base class as part of the implementation for one of
my classes. In my implementation, it holds cached data, and I'd like to
be able to call its const member functions from my nonconst members. Is
there a way to get something like "mutable inheritance" to accomplish this?


I don't see how calling const member functions from non-const ones should
present a problem in of itself, independent of any inheritance
configuration. It's the other way 'round that's problematic. This is pretty
abstract to me; can you express what you're trying to do with code?
Thanks,
-leor
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #2
Leor Zolman wrote:
On Fri, 02 Apr 2004 14:33:34 -0700, "Adam H. Peterson" <ah**@email.byu.edu>
wrote:

I'm using a private base class as part of the implementation for one of
my classes. In my implementation, it holds cached data, and I'd like to
be able to call its const member functions from my nonconst members. Is
there a way to get something like "mutable inheritance" to accomplish this?

I don't see how calling const member functions from non-const ones should
present a problem in of itself, independent of any inheritance
configuration. It's the other way 'round that's problematic. This is pretty
abstract to me; can you express what you're trying to do with code?
Thanks,
-leor


Yes, sorry, that's what I meant.

class HelperDataStructure {
public:
void addData(T const &data);
T const &getData(int) const;
};

class MyType
: private HelperDataStructure {
public:
U const &someIndexingOperation(T const &data) const {
// E.g. this function call (addData()) doesn't
// change the observable behavior of the class.
// It just remembers something for later as an
// optimization.
HelperDataStructure::addData(data);
// ...
return U(getData());
}
// ...
};
I know this isn't complete code, but I think it illustrates the idea.

Thanks,
Adam Peterson
Jul 22 '05 #3
On Fri, 02 Apr 2004 16:05:29 -0700, "Adam H. Peterson" <ah**@email.byu.edu>
wrote:
Leor Zolman wrote:
On Fri, 02 Apr 2004 14:33:34 -0700, "Adam H. Peterson" <ah**@email.byu.edu>
wrote:

I'm using a private base class as part of the implementation for one of
my classes. In my implementation, it holds cached data, and I'd like to
be able to call its const member functions from my nonconst members. Is
there a way to get something like "mutable inheritance" to accomplish this?

I don't see how calling const member functions from non-const ones should
present a problem in of itself, independent of any inheritance
configuration. It's the other way 'round that's problematic. This is pretty
abstract to me; can you express what you're trying to do with code?
Thanks,
-leor


Yes, sorry, that's what I meant.


I thought so, but this was hazy enough to me that I figured I ought to make
sure before proceeding. Okay, all I can think of is sort of the "obvious"
hack:

class HelperDataStructure {
public:
void addData(T const &data);
T const &getData(int) const;
};

class MyType
: private HelperDataStructure {
public:
U const &someIndexingOperation(T const &data) const {
// E.g. this function call (addData()) doesn't
// change the observable behavior of the class.
// It just remembers something for later as an
// optimization.
HelperDataStructure::addData(data);


(const_cast<MyType *>(this))->HelperDataStructure::addData(data);

Basically, as I see it, if /you know what you're doing/ in the sense that
nothing happening up there is "really" going to violate the constness of
your object, it can just be your dirty little secret ;-)

Personally, I hope someone comes up with a better solution for you.
-leor

--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #4
"Adam H. Peterson" <ah**@email.byu.edu> wrote in message
news:c4***********@acs2.byu.edu...
Leor Zolman wrote:
On Fri, 02 Apr 2004 14:33:34 -0700, "Adam H. Peterson" <ah**@email.byu.edu> wrote:

I'm using a private base class as part of the implementation for one of
my classes. In my implementation, it holds cached data, and I'd like to
be able to call its const member functions from my nonconst members. Is
there a way to get something like "mutable inheritance" to accomplish
this?

I don't see how calling const member functions from non-const ones should present a problem in of itself, independent of any inheritance
configuration. It's the other way 'round that's problematic. This is pretty abstract to me; can you express what you're trying to do with code?
Thanks,
-leor


Yes, sorry, that's what I meant.

class HelperDataStructure {
public:
void addData(T const &data);
T const &getData(int) const;
};

class MyType
: private HelperDataStructure {
public:
U const &someIndexingOperation(T const &data) const {
// E.g. this function call (addData()) doesn't
// change the observable behavior of the class.
// It just remembers something for later as an
// optimization.
HelperDataStructure::addData(data);
// ...
return U(getData());
}
// ...
};
I know this isn't complete code, but I think it illustrates the idea.

Thanks,
Adam Peterson


How about using composition rather than inheritance:

class MyType
{
private:
mutable HelperDataStructure m_helper;
public:
U const &someIndexingOperation(T const &data) const {
// E.g. this function call (addData()) doesn't
// change the observable behavior of the class.
// It just remembers something for later as an
// optimization.
m_helper.addData(data);
// ...
return U(m_helper.getData());
}
// ...
};

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #5
>>class HelperDataStructure {
public:
void addData(T const &data);
T const &getData(int) const;
};

class MyType
: private HelperDataStructure {
public:
U const &someIndexingOperation(T const &data) const {
// E.g. this function call (addData()) doesn't
// change the observable behavior of the class.
// It just remembers something for later as an
// optimization.
HelperDataStructure::addData(data);

(const_cast<MyType *>(this))->HelperDataStructure::addData(data);

Basically, as I see it, if /you know what you're doing/ in the sense that
nothing happening up there is "really" going to violate the constness of
your object, it can just be your dirty little secret ;-)

Personally, I hope someone comes up with a better solution for you.


I hope so too. Thanks for taking the time to respond, btw.

I don't like this approach since it's an awful lot like the hacks people
used to use for constness before we had "mutable" in the first place.
But I suppose if we don't have mutable base classes, then I guess the
same hacks are needed again. Perhaps I'll just refactor it so the
derived class uses aggregation instead of inheritance and stub the
inherited functions. At least I'm not depending on any polymorphism
from the private base (this time).

Thanks anyway,
Adam Peterson
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

50
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong...
3
by: Thomas Matthews | last post by:
Hi, I understand that a member function can be declared as const {meaning it doesn't alter the member variables), but can it be declared as mutable? I have class that stores its members into...
5
by: Adam H. Peterson | last post by:
I'm using a private base class as part of the implementation for one of my classes. In my implementation, it holds cached data, and I'd like to be able to call its const member functions from my...
3
by: Ingo Nolden | last post by:
Hi, I try to use const where ever appropriate. In a collection class I am counting the iterators that are out. The counter decrements when an iterator leaves scope or is 'Dispose( )'d While...
6
by: christopher diggins | last post by:
I wrote a dynamic matrix class similar to the one described in TCPL 3rd Edition. Rather than define two separate iterators for const and non-const scenarios I decided to be a lazy bastard and only...
12
by: Vincent RICHOMME | last post by:
Hi, I am currently implementing some basic classes from .NET into modern C++. And I would like to know if someone would know a non mutable string class.
1
by: Let_Me_Be | last post by:
Hi, I'm kinda worndering how the mutable keyword works. Because of speed, I added value caching into my base class, and the only posible way to implement this seemed to be adding the mutable...
35
by: bukzor | last post by:
I've found some bizzare behavior when using mutable values (lists, dicts, etc) as the default argument of a function. I want to get the community's feedback on this. It's easiest to explain with...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.