Connecting Tech Pros Worldwide Forums | Help | Site Map

accessing multiple properties through a single set/get

Amit Limaye
Guest
 
Posts: n/a
#1: Mar 2 '06
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


Ben Pope
Guest
 
Posts: n/a
#2: Mar 2 '06

re: accessing multiple properties through a single set/get


Amit Limaye wrote:[color=blue]
> 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.[/color]

Do it differently.
[color=blue]
> 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[/color]

Member offsets?
[color=blue]
> thing rips apart everything C++/OOPS stands for. This is how i would do[/color]

I don't see how providing set and get for 15 private members of a class
is not ripping apart OOP.
[color=blue]
> it in C but C++ does it have a better method.
> I dont want to go the RTTI way[/color]

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...
Amit Limaye
Guest
 
Posts: n/a
#3: Mar 2 '06

re: accessing multiple properties through a single set/get


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

Dietmar Kuehl
Guest
 
Posts: n/a
#4: Mar 3 '06

re: accessing multiple properties through a single set/get


Amit Limaye wrote:[color=blue]
> 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.[/color]

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:dietmar_kuehl@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Amit Limaye
Guest
 
Posts: n/a
#5: Mar 3 '06

re: accessing multiple properties through a single set/get


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

Closed Thread


Similar C / C++ bytes