Casting to const.

J

Jason Heyes

I didn't want to repeat the same code so I casted to const. Here is a
simplified example of when casting to const can help avoid code duplication:

class MyClass
{
int numbers[50];

public:
// default constructor not shown

int &find_largest()
{
return ((const MyClass &)*this).find_largest();
}

const int &find_largest() const; // implementation not shown
};

Should I be doing this? Is there a better way?

I wanted to do this in another situation but it doesn't look possible. Here
it is:

class MyOtherClass
{
std::vector<int> numbers;

public:
// default constructor not shown

void find_even(std::vector<int *> &even) {
for (std::vector<int>::size_type i=0; i < numbers.size(); i++) {
if (numbers % 2)
even.push_back(&numbers);
}
}

void find_even(std::vector<const int *> &even) const {
for (std::vector<int>::size_type i=0; i < numbers.size(); i++) {
if (numbers % 2)
even.push_back(&numbers);
}
}
};

Notice that the code in both methods is exactly the same. How do I avoid
this kind of duplication? Thanks.
 
J

Jason Heyes

I just realised that I could have written the following.

class MyClass
{
int numbers[50];
int &find_largest_private() const; // implementation not shown
public:
int &find_largest() { return find_largest_private(); }
const int &find_largest() const { return find_largest_private(); }
};

I don't know if I want to do that though. What do you guys think?
 
P

Phlip

Jason said:
I just realised that I could have written the following.

class MyClass
{
int numbers[50];
int &find_largest_private() const; // implementation not shown
public:
int &find_largest() { return find_largest_private(); }
const int &find_largest() const { return find_largest_private(); }
};

I don't know if I want to do that though. What do you guys think?

I thought of this summer (coming back to C++ after refactoring duplication
out of Python and such mercilessly), and was waiting for a way to illustrate
it.

Thanks! it's perfect! It illustrates these design goals:

- don't return mutable handles to private data
inside constant objects

- don't duplicate behavior (your "implementation")
 
R

Ron Natalie

Jason Heyes said:
I just realised that I could have written the following.

class MyClass
{
int numbers[50];
int &find_largest_private() const; // implementation not shown

It's not clear what find_largest_const is going to return.
It can't return a reference to something in numbers without some headstanding
because the type of numbers is effecitvely const int[50] inside a const method.

How about
class MyClass {
int find_largest_private_index() const; // returns the index 0...50 of the larges

int& find_largest() { return numbers[find_largest_private_index()] };
const int& find_largest() const { return numbers[find_largest_private_index()] };
 
J

Jason Heyes

Ron Natalie said:
It's not clear what find_largest_const is going to return.
It can't return a reference to something in numbers without some headstanding
because the type of numbers is effecitvely const int[50] inside a const method.

How about
class MyClass {
int find_largest_private_index() const; // returns the index 0...50 of the larges

int& find_largest() { return numbers[find_largest_private_index()] };
const int& find_largest() const { return
numbers[find_largest_private_index()] };

Yes my mistake. I forgot that numbers was const within
find_largest_private(). Thanks for that.
 
A

Andrey Tarasevich

Jason said:
I just realised that I could have written the following.

class MyClass
{
int numbers[50];
int &find_largest_private() const; // implementation not shown
public:
int &find_largest() { return find_largest_private(); }
const int &find_largest() const { return find_largest_private(); }
};

I don't know if I want to do that though. What do you guys think?
...

This is no better than your original approach (which, BTW, is an
acceptable way to avoid code duplication), since in general case you'll
eventually discover that you can't return a non-const 'int&' from a
const method 'find_largest_private' without an explicit 'const_cast'.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top