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

hiding function call under the "instance->member" notation

We have zillion of instances inf instance->m_member in the code.
We are going introduce the 'accessors' Get() and Set() for m_member,
and
m_member going private (and renamed, too). We would like to leave, if
possible,
refs inst->m_member in the code, but make compiler translate
it to inst->Get() invisily, and without using macros. Let's assume T
is
a type of m_member.

Is there any c++ trick to redirect inst->m_foo /* no parentheses
after m_foo */
to function call inst->Get(), without using macros ? I am sure there
is some
trick of declaring m_foo a special small class that evaluates a
function
when accessed, but I can't figure it out.

Apparently semi-possible with #define (only outside of the class):
#define m_member Get()
but that sucks
(you do not need to enumerate all reasons why it sucks, we are not
going to use it; this just to illustrates equivalent of what we
want).
We do not want to use macro, we want the C++ equivalent.

V.M.
Jun 27 '08 #1
4 1606
On 15 huhti, 09:15, viki <viki...@gmail.comwrote:
We do not want to use macro, we want the C++ equivalent.
You are greys aren't you? You guys just don't get how
obviously you are space aliens:)
Jun 27 '08 #2
viki wrote:
We have zillion of instances inf instance->m_member in the code.
We are going introduce the 'accessors' Get() and Set() for m_member,
Why?

Ge/set accessors are invariably a design smell. Unless they do
something other than just getting and setting the value, they are a smell.

--
Ian Collins.
Jun 27 '08 #3
* viki:
We have zillion of instances inf instance->m_member in the code.
We are going introduce the 'accessors' Get() and Set() for m_member,
and
m_member going private (and renamed, too). We would like to leave, if
possible,
refs inst->m_member in the code, but make compiler translate
it to inst->Get() invisily, and without using macros. Let's assume T
is
a type of m_member.

Is there any c++ trick to redirect inst->m_foo /* no parentheses
after m_foo */
to function call inst->Get(), without using macros ? I am sure there
is some
trick of declaring m_foo a special small class that evaluates a
function
when accessed, but I can't figure it out.

Apparently semi-possible with #define (only outside of the class):
#define m_member Get()
but that sucks
(you do not need to enumerate all reasons why it sucks, we are not
going to use it; this just to illustrates equivalent of what we
want).
We do not want to use macro, we want the C++ equivalent.
As others have remarked else-thread, what you have is a very foul design-smell.

However, at the technical level (this is perhaps akin to answering a question
about how to use goto, and note that the language constructs involved are at the
same level):
<code>
#include <iostream // std::cout, std::ostream
#include <ostream // operator<<, std::endl
#include <memory // std::auto_ptr

#ifdef _MSC_VER
#pragma warning( disable: 4355 ) // 'this' used in initializer list.
#endif

class Foo
{
public:
int member;
Foo(): member( 42 ) {}
};

class Bar
{
private:
template<
typename T,
void (Bar::*set)( T const& ),
T (Bar::*get)() const
>
class GetSetProxy
{
private:
Bar* myBar;
public:
GetSetProxy( Bar* pBar ): myBar( pBar ) {}

GetSetProxy& operator=( T const& v )
{
(myBar->*set)( v ); return *this;
}

operator T() const { return (myBar->*get)(); }
};

int myMember;

void setMember( int const& x ) { myMember = x; }
int getMember() const { return myMember; }
typedef GetSetProxy<int, &Bar::setMember, &Bar::getMember Member;

public:
Member member;

Bar(): myMember( 42 ), member( this ) {}
};

int main()
{
using namespace std;

std::auto_ptr<Foo pFoo( new Foo );
pFoo->member = 666;
cout << pFoo->member << endl;

std::auto_ptr<Bar pBar( new Bar );
pBar->member = 777;
cout << pBar->member << endl;
}
</code>
Note that GetSetProxy::operator=() isn't 'const', because in this particular
scenario, although assignment doesn't modify the proxy object, the constness of
the containing object is transferred to the proxy and should determine whether
assignment is allowed.
Cheers, & hope you tackle the design rather than doing silly things like above,

- Alf

Jun 27 '08 #4
viki <vi*****@gmail.comwrote in news:aa6f7bf3-8fd3-4c2f-a716-
d9**********@p25g2000hsf.googlegroups.com:
We have zillion of instances inf instance->m_member in the code.
We are going introduce the 'accessors' Get() and Set() for m_member,
and
m_member going private (and renamed, too). We would like to leave, if
possible,
refs inst->m_member in the code, but make compiler translate
it to inst->Get() invisily
Why? If you succeed in your pursuit, then you will have zillion lines of
code which do not do what they appear to do.

I have found that in long run it is far better to spend some time on
Find/Replace and have a clean code afterwards. If your code has m_member
names elsewhere, it takes a bit more care and time: first you make the
member private, then recompile. You spot the failing access patterns,
replace them in all files by global find-replace, recompile, repeat until
compile errors have gone, then run the unit tests.

In short term, you could also rename the member, make it private and
provide a const reference m_member to the real member. This ensures that
one cannot accidentally change the member value, but as you have not
stated your goal I cannot say if this would be an acceptable solution.

Regards
Paavo
Jun 27 '08 #5

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

Similar topics

4
by: Jian H. Li | last post by:
Hello, What's the essential differences between the two ways of "class::member" & "object.member"(or object_pointer->member)? class C{ public: void f() {} int i; };
4
by: seesaw | last post by:
class A { public: static A* newA() { return new A; } .... }; In the code, two things not very clear and natural to me: 1. the method newA() is defined as static. 2. newA as a member method...
8
by: kevin | last post by:
I have a form and in the form I have a sub that uses a class I instantiate using visual basic code: Public oCP As New Rs232 'instantiate the comm port I need to share this sub with...
7
by: Søren Dreijer | last post by:
Hi, I have a mixed C#, managed C++ and unmanaged C++ project. The managed class calls a method which exists in an unmanaged singleton class. During the entire lifetime of the application, this...
37
by: jht5945 | last post by:
For example I wrote a function: function Func() { // do something } we can call it like: var obj = new Func(); // call it as a constructor or var result = Func(); // call it as...
5
by: Jon E. Scott | last post by:
I'm a little confused with "static" methods and how to access other unstatic methods. I'm a little new to C#. I'm testing a callback routine within a DLL and the callback function returns a...
94
by: Samuel R. Neff | last post by:
When is it appropriate to use "volatile" keyword? The docs simply state: " The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock...
28
by: Jess | last post by:
Hello, It is said that if I implement a "swap" member function, then it should never throw any exception. However, if I implement "swap" non- member function, then the restriction doesn't...
3
by: MagicKat | last post by:
Can you explain what they mean by "instant member" for instance, in the article about arraylists, it says it's thread safe but it's instant members are not. What does that mean? ...
11
by: eBob.com | last post by:
I have this nasty problem with Shared methods and what I think of as "global storage" - i.e. storage declared outside of any subroutines or functions. In the simple example below this "global"...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.