shared data between classes

From: ma740988 (ma740988_at_pegasus.cc.ucf.edu)
Date: 11/19/03


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;



Relevant Pages

  • Re: C++ Initialisation List
    ... > I end all data members with an underscore. ... > Point(int x, int y); ... what's the reason for "ending all data members with an ...
    (comp.lang.cpp)
  • poker odds
    ... public static final int HIGH_CARD = 0; ... private static Logger logger; ... public Poker(int numCard, Card deck) ... public void maxSimulation ...
    (comp.lang.java.programmer)
  • problem with vmr9 image compositor - method Draw
    ... I play video with playlist, if i play video about 5x it is ... but so 6round of video playing throw exception int ... private void ConfigureVMR9InWindowlessMode() ... Surface renderTarget = new Surface; ...
    (microsoft.public.win32.programmer.directx.managed)
  • Re: using ref keyword performance
    ... // private fields... ... int valueCount = values.Length; ... static void TestWithRefLoop() ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: The code for "sorting in C sharp considered superior" in five parts for easier download
    ... private bool interchangeSortTest() ... ("Sequence error after interchange sort", ... private bool labelHasFontTag ... int intGridTop, ...
    (comp.programming)

Loading