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

How to force a call for an inspector

Hi all,

Imagine you have a class, containing two methods
with the same name (say 'get'), but one being an
inspector the other mutator - the class contains
a kind of data structure (vector in the example below,
but more complicated in the real-life example) and
for 'incorrect' calls the inspector returns zero
but the mutator expands the storage.

Now imagine that this function is called from inside
another method (say 'foo') which is a mutator (and must
be such) but that should not change the size of data
structure described above. How to force this method
to call the INSPECTOR 'get' (and prevent the expansion
of data structure).

Example code at the end of this post.

I have two ideas but I hope that there is a better
solution:
a) make the method 'foo' const and make all the parts
it changes mutable - but I don't like it, in the
real-life case I have in mind this will obscure the code
and sure abuse the mutab keyword
b) use two names for inspector/mutator get - this is
what I do right now, but it has disadvantages: the methods
cannot be operators () or [] then, it may be unclear for
users of the code why these two methods have different names.

Thus, I will apreciate any suggestions. Thanks in advance,
Przemek
Example code:
============
#include <iostream>
#include <vector>
#include <stdexcept>

class A {
private:
// some container e.g. a vector
std::vector<intvec;
// whatever else
int bar;
public:
// example c-tor
A() : vec(10, 0) {};
// two get methods
int get(int) const;
int & get(int);
// another method that MUST be mutator
void foo(int);
//...
};

// inspector get
// return 0 outside the storage space
int A::get(int i) const
{
if( i < 0 ) throw std::out_of_range("A::get");
if( static_cast<unsigned>(i) >= vec.size() ) return 0;
return vec[i];
}

// mutator get
// EXPANDS the storage when accesing past the end
int & A::get(int i)
{
std::cout << "mutator get here" << std::endl;
if( i < 0 ) throw std::out_of_range("A::get");
if( static_cast<unsigned>(i) >= vec.size() )
vec.resize(i + 1, 0);
return vec[i];
}

// a method that MUST be mutator
void A::foo(int i)
{
// this calls mutator 'get'
// but how to call the inspector???
bar += get(i);
}

int main()
{
A a;
a.foo(15);
return 0;
}

Jan 12 '08 #1
1 1974
Przemyslaw Koprowski wrote:
Hi all,

Imagine you have a class, containing two methods
with the same name (say 'get'), but one being an
inspector the other mutator - the class contains
a kind of data structure (vector in the example below,
but more complicated in the real-life example) and
for 'incorrect' calls the inspector returns zero
but the mutator expands the storage.

Now imagine that this function is called from inside
another method (say 'foo') which is a mutator (and must
be such) but that should not change the size of data
structure described above. How to force this method
to call the INSPECTOR 'get' (and prevent the expansion
of data structure).

Example code at the end of this post.

I have two ideas but I hope that there is a better
solution:
a) make the method 'foo' const and make all the parts
it changes mutable - but I don't like it, in the
real-life case I have in mind this will obscure the code
and sure abuse the mutab keyword
b) use two names for inspector/mutator get - this is
what I do right now, but it has disadvantages: the methods
cannot be operators () or [] then, it may be unclear for
users of the code why these two methods have different names.

Thus, I will apreciate any suggestions. Thanks in advance,
Przemek
Example code:
============
#include <iostream>
#include <vector>
#include <stdexcept>

class A {
private:
// some container e.g. a vector
std::vector<intvec;
// whatever else
int bar;
public:
// example c-tor
A() : vec(10, 0) {};
// two get methods
int get(int) const;
int & get(int);
// another method that MUST be mutator
void foo(int);
//...
};

// inspector get
// return 0 outside the storage space
int A::get(int i) const
{
if( i < 0 ) throw std::out_of_range("A::get");
if( static_cast<unsigned>(i) >= vec.size() ) return 0;
return vec[i];
}

// mutator get
// EXPANDS the storage when accesing past the end
int & A::get(int i)
{
std::cout << "mutator get here" << std::endl;
if( i < 0 ) throw std::out_of_range("A::get");
if( static_cast<unsigned>(i) >= vec.size() )
vec.resize(i + 1, 0);
return vec[i];
}

// a method that MUST be mutator
void A::foo(int i)
{
// this calls mutator 'get'
// but how to call the inspector???
bar += get(i);
try

bar += const_cast< A const * >( this )->get(i);
However: the use of the cast may indicate a deeper design problem.
}

int main()
{
A a;
a.foo(15);
return 0;
}

Best

Kai-Uwe Bux
Jan 12 '08 #2

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

Similar topics

1
by: Charles Soto | last post by:
I've got a main loop script that calls two other scripts that do no user interaction. All they do is send a couple of mysql update statements. Then they use header() to call the main loop again. ...
0
by: Jokeline | last post by:
=====PRANK CALL===== How to use this service: 1. Choose a Prank from the list below 2. Make sure you have the telephone number of your chosen target 3. Call 0906 403 8796 4. Listen in as the...
2
by: Brian | last post by:
Hi all, So, I posted a question about a DOM inspector for IE (mozilla comes with one, which is pretty nice). Since I got no reply, I wrote my own, which works pretty well. It is laid out...
6
by: Alex Vassiliev | last post by:
Hi Just wondering if anybody can recommend the best JavaScript based DOM browser / inspector? Like those ones: http://www.informatrix.ch/joe/joe.html...
4
by: Robert Skidmore | last post by:
I made this javascript to help me debug some javascript one time and I have just been adding to it over the years. It is IE specific (sure it would not be to hard to change that). It is not ever...
0
by: Martin Knauer | last post by:
Hi, I try to build an Outlook Plugin. After having some trouble with the Inspectorevents I'm trying to build a wrapper for the Inspectorclass. However I'm stuck again after letting VS.Net 2003...
3
by: Adriano Coser | last post by:
Hello. Does anyone know an "Object inspector" component for VC++ .NET? I'm looking for a component that I can use to edit the properties of drawing elements on my CAD application. I'm...
3
by: Arulraja | last post by:
Hello, I have created 2 custom server controls, The parent custom control contains multiple child custom controls. The Child control has a button on it. If I Click the button control, it...
6
by: bryanbabula | last post by:
I have a question about overriding i was wondering if anyone could help me with, or even suggesting a better/different way. I have no idea if this can even be done or not. I was wondering if there...
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: 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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.