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

accessing multiple properties through a single set/get

Hello,
i have a class with around 10-15 properties and i want to write
a single set get pair to access all these properties. Any suggestions.
BTW this is not some question for an interview. I was thinking of
implementing this in some of the code i have been writing.
I was asked this question a couple of years ago and i jumped onto
member offsets since the class is laid out as a structure. But tht
thing rips apart everything C++/OOPS stands for. This is how i would do
it in C but C++ does it have a better method.
I dont want to go the RTTI way

BTW does C++ use RTTI even when i do TypeID for native data types and
not objects

-SIGTERM
amit

Mar 2 '06 #1
4 1899
Amit Limaye wrote:
Hello,
i have a class with around 10-15 properties and i want to write
a single set get pair to access all these properties. Any suggestions.
Do it differently.
BTW this is not some question for an interview. I was thinking of
implementing this in some of the code i have been writing.
I was asked this question a couple of years ago and i jumped onto
member offsets since the class is laid out as a structure. But tht
Member offsets?
thing rips apart everything C++/OOPS stands for. This is how i would do
I don't see how providing set and get for 15 private members of a class
is not ripping apart OOP.
it in C but C++ does it have a better method.
I dont want to go the RTTI way


What would the RTTI way be?
It sounds to me like your class is attempting to do too much. 15
members is quite a few. Additionally, I would suggest that you could
probably throw it away completely.

Instead of providing accessors for each property, you should probably
house the members that a class requires access in the class that
requires it.

What are you trying to achieve?

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Mar 2 '06 #2
Ben i am trying to provide a single set API for all SNMP supported data
types so tht the end user need to know the details of the SNMP property
and the likes.
About member offsets.
I have used the offsetof macro in C to access different members of a
structure or getting function pointers in a structure.

In the existing problem i dont want to start providing API like
setTcpWindowSize, SetMaximumsegementsize for controlling TCP behavior
using SNMP.
I want to tie al properties related to TCP in a single class and
provide a single set to access all these properties individually
-SIGTERM
amit

Mar 2 '06 #3
Amit Limaye wrote:
i have a class with around 10-15 properties and i want to write
a single set get pair to access all these properties. Any suggestions.


Assuming that all members have a common type, you can use a bit of
template trickery:

#include <iostream>

class foo
{
public:
foo(): A(1), B(2), C(3) {}

enum member
{
memberA,
memberB,
memberC
};

template <member m> int get() const
{
return this->*members[m];
}

template <member m> void set(int v)
{
this->*members[m] = v;
}

private:
static int foo::*members[];
int A;
int B;
int C;
};

int foo::*foo::members[] = {
&foo::A,
&foo::B,
&foo::C
};

int main()
{
foo f;
std::cout << f.get<foo::memberA>() << " "
<< f.get<foo::memberB>() << " "
<< f.get<foo::memberC>() << "\n";

f.set<foo::memberB>(4);

std::cout << f.get<foo::memberA>() << " "
<< f.get<foo::memberB>() << " "
<< f.get<foo::memberC>() << "\n";
}

If the member types are different, things get really messy although
I think that with some template meta programming you can achieve
even this.

Since someone complained that this is ripping apart OOP: I don't
think it does and I can imagine reasons to do things this way.
However, personally, I would probably rather channel the various
member functions through one internal function.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Mar 3 '06 #4
Wht u suggest surely does not rip apart the encapsulation or data
hiding approach.
What i had initially used as a solution of calculating offsets of data
members from base address of class pointer did do it

-SIGTERM
amit

Mar 3 '06 #5

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

Similar topics

9
by: Graham | last post by:
I have been having some fun learning and using the new Controls and methods in .Net 2.0 which will make my life in the future easier and faster. Specifically the new databinding practises and...
6
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing...
10
by: Kishor | last post by:
Hi, I am looking for the solution to my problem. this is related to inheritance (Inheriting properties of base class in multiple quantity) . I am writing code in VB.net language, I am having two...
0
by: sonu | last post by:
I have following client side code which i have used in my asp.net project SummaryFeatured Resources from the IBM Business Values Solution Center WHITEPAPER : CRM Done Right Improve the...
6
by: evandelagrammaticas | last post by:
Hi all. I have spent the better part of a day scouring the newsgroups and I am sure that I must have come across the solution a number of times - but I am still a real newbie at asp.net so please...
4
by: vze1r2ht | last post by:
I have many types of classes and I'm deciding whether to use a single class or multiple classes for EACH type of class. For an example: User class has 3 classes associated with it: User...
4
by: Diffident | last post by:
Hello All, IIS 6.0 - I have an application which resides in its own application pool. Does anyone know if we can create multiple application pools for the same application? If we can create...
47
by: Larry Smith | last post by:
I just read a blurb in MSDN under the C++ "ref" keyword which states that: "Under the CLR object model, only public single inheritance is supported". Does this mean that no .NET class can ever...
2
by: Dahak | last post by:
It seems that my GoogleFu has failed me tonight and I'm hoping I can find some advice. I've inherited a ASP.NET project written in Visual Basic. I'm not too familiar with the .NET framework...
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:
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: 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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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...

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.