473,387 Members | 1,925 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,387 software developers and data experts.

Casting to const.

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
5 1528
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
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

"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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Trevor Lango | last post by:
I want to be able to cast away the constness of a private member variable in a member function of a class. I have the private data member declared as follows: const double x; I have an...
13
by: JustSomeGuy | last post by:
I have two object types ClassA and ClassB class ClassA { public: int data; operator ClassB() { ClassB b; b.data = data + 1; return (b);
5
by: Ingo Nolden | last post by:
Hi there, I am writing a smart pointer that is similar to the boost intrusive ptr. I am trying to make it behave like a c++ pointer including the implicit and explicit casting behaviour. Below...
0
by: Greg | last post by:
Not sure if this is best place for this problem, but here it is. I have a project that is simply a C# class that interfaces with an IFilter. This is so I can retreive the text from Word docs. ...
8
by: Herby | last post by:
Given class B and C which inherit from class A They all override a method of the form: Add( A^ lhs, A^ rhs ); So A is abstract. So if i was defining Add for class B : B::Add( A^ lhs, A^ rhs...
11
by: Vinod | last post by:
Hi, I am working in the project where VC6 code is ported to VC8 (VC++ .Net 2005) I got a problem when I cast a double value to unsigned int. Problem is I couldn’t get the proper value after...
5
by: brekehan | last post by:
I've always been a little sketchy on the differences between static, dynamic, and reinterpret casting. I am looking to clean up the following block by using C++ casting instead of the C style...
17
by: sophia.agnes | last post by:
Hi , I was going through peter van der linden's book Expert C programming, in this book there is a section named "How and why to cast" the author then says as follows (float) 3 - it's a...
5
by: jason.cipriani | last post by:
There have been some recent threads about casting pointers to and from void* that have me rethinking some of my usual practices. I have a couple of questions. 1. What is the purpose of C++'s...
10
by: Alex Vinokur | last post by:
Hi, Is it possible to do C++-casting from const pair<const unsigned char*, size_t>* to const pair<unsigned char*, size_t>* ? Alex Vinokur
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.