By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,171 Members | 1,020 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,171 IT Pros & Developers. It's quick & easy.

Casting to const.

P: n/a
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[i] % 2)
even.push_back(&numbers[i]);
}
}

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

Notice that the code in both methods is exactly the same. How do I avoid
this kind of duplication? Thanks.
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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?

Jul 22 '05 #2

P: n/a
Jason Heyes wrote:
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")

--
Phlip
http://www.c2.com/cgi/wiki?TestFirstUserInterfaces
Jul 22 '05 #3

P: n/a

"Jason Heyes" <ge******@optusnet.com.au> wrote in message news:3f**********************@news.optusnet.com.au ...
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()] };
Jul 22 '05 #4

P: n/a
"Ron Natalie" <ro*@sensor.com> wrote in message
news:3f*********************@news.newshosting.com. ..

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.
Jul 22 '05 #5

P: n/a
Jason Heyes wrote:
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'.

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.