473,785 Members | 3,245 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1635
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::getMem ber Member;

public:
Member member;

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

int main()
{
using namespace std;

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

std::auto_ptr<B ar pBar( new Bar );
pBar->member = 777;
cout << pBar->member << endl;
}
</code>
Note that GetSetProxy::op erator=() 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**********@p2 5g2000hsf.googl egroups.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
2499
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
4333
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 of class A IS returning a "new" instance of A
8
11441
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 another form so to declare the sub I use visual basic code: Public Shared Function IsPortAvailable(ByVal ComPort As Integer) As Boolean
7
9538
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 gives no problems whatsoever. However, upon shutdown an exception pops up: "The string binding is invalid" If I call the singleton method from inside a purely unmanaged class, I
37
3988
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 a function
5
8909
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 string as one of the arguments. As shown below, I have no problems showing the string in the Report() method in a messagebox or console window, but how does one access a memo or label on a form from a static method? I want to throw the strings from...
94
30354
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 Statement (C# Reference) statement to serialize access. " But when is it better to use "volatile" instead of "lock" ?
28
2857
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 apply. Can somebody tell me why? Thanks, Jess
3
1427
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? http://msdn2.microsoft.com/en-us/library/system.collections.arraylist.aspx
11
3166
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" storage is ButtonHasBeenClicked. In this simple example code in Form1 calls a routine in Module1 which then calls code back in Form1 (subroutine WhatEver). WhatEver needs to access ButtonHasBeenClicked but the reference to ButtonHasBeenClicked...
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10325
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10148
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9950
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8972
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.