By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,187 Members | 1,578 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,187 IT Pros & Developers. It's quick & easy.

accessing multiple properties through a single set/get

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.