mutable base class

  • Thread starter Adam H. Peterson
  • Start date
A

Adam H. Peterson

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?
 
L

Leor Zolman

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
 
A

Adam H. Peterson

Leor said:
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
 
L

Leor Zolman

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
 
C

Cy Edmunds

Adam H. Peterson said:
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());
}
// ...
};
 
A

Adam H. Peterson

class HelperDataStructure {
(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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top