shared data between classes
From: ma740988 (ma740988_at_pegasus.cc.ucf.edu)
Date: 11/19/03
- Next message: S Perryman: "Re: Test Driven Development Sample (Yet Another Pet Example)"
- Previous message: Ray Laa: "Re: OO Component Book?"
- Next in thread: Cagdas Ozgenc: "Re: shared data between classes"
- Reply: Cagdas Ozgenc: "Re: shared data between classes"
- Reply: EventHelix.com: "Re: shared data between classes"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 19 Nov 2003 02:28:04 -0800
Trying to get a feel for how to access private data members without
having to write an accessor/mutator functions for each data member? I
could make them public but - the consesus is that public data members
is bad news.
Consider
class CommonStuff
{
public:
CommonStuff() {};
void Increment (unsigned int which);
private:
enum NUM_COUNTS = 4;
int counts[NUM_COUNTS];
};
void CommonStuff::Increment (unsigned int which)
{
if (which < NUM_COUNTS)
++counts[which];
}
int main ()
{
CommonStuff cs;
cs.Increment (1);
}
// the above example used a 'magic number' as a passed param to
increment.
But now increase the scope. i have lots of counters, struct and a
template class that'll get initialized in CommonStuff and manipulated
by second class 'FOO_WORKER' (not shown) so now.
// so now
class CommonStuff
{
private:
int counter1, counter2, counter3, counter4, counter5, counter6;
//etc to 20+
struct MSG_STRUCT1 {
int abc;
int def;
}
struct MSG_STRUCT2 {
int aaa;
int bbb;
}
MyTemplate< MSG_STRUCT2 > msg_struct; // see below for this
public:
CommonStuff() : counter1(0), counter2(0),
counter3(0), counter4(0),
counter5(0), counter6(0)
{
}
// 1. How would i implement Increment for - the sake of argument
// - 6 counters without (6) accessor and (6) mutators and without
// vectors? Lets assume FOO_WOKER wants to increment counter3
// void Increment( )
// 2. Same story for MyTemplate, lets say FOO_WORKER wants to call the
get_size function which is now private. how would i put an interface
around this?
// 3. Like wise access to the structs - for now say MSG_STRUCT1.
};
#ifndef MyTemplate_H
#define MyTemplate_H
template <class T>
class MyTemplate
{
public:
MyTemplate();
~MyTemplate();
T Get_size() { std::cout << " get_size member function " <<
std::endl; }
private:
static const int SIZE = 4;
T test[ SIZE ];
};
#endif;
- Next message: S Perryman: "Re: Test Driven Development Sample (Yet Another Pet Example)"
- Previous message: Ray Laa: "Re: OO Component Book?"
- Next in thread: Cagdas Ozgenc: "Re: shared data between classes"
- Reply: Cagdas Ozgenc: "Re: shared data between classes"
- Reply: EventHelix.com: "Re: shared data between classes"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|