473,325 Members | 2,828 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,325 software developers and data experts.

Keeping one data structure through multiple classes.

158 100+
I might be going at this wrong but here goes. I have a program with multiple classes, each of which I initialize in main.cpp

I have a data structure that keeps track of the parameters for the program, defined in a file called Defines.h. All classes include this file.

My structure is similar to this.
Expand|Select|Wrap|Line Numbers
  1. struct params {
  2.   int paramone;
  3.   float paramtwo;
  4. };
  5.  
In main I create a pointer to a params object like:
Expand|Select|Wrap|Line Numbers
  1. params* pointerToParams;
  2.  
This all works fine so far. What I want to happen is have this pointerToParams available to all my other classes and where ever it needs it in those classes.

I first thing i did was in each classes initialize function it took a pointer to params as a parameter. This works. Then I made it that each class also has a private pointer variable of prams which I set equal to the passed param pointer.

My problem is when I want to change a variable in the params structure in any class I want the changes to be reflected in all classes. So in short I only want one instance of params to be available anywhere in the program.

Any thoughts,
Thanks for the help.
May 4 '10 #1
7 2931
weaknessforcats
9,208 Expert Mod 8TB
You could create a Singleton object that is accessible from all of your other classes. This is, in effect a global object. Read the article in the C/C++ Insights for the Singleton Design Pattern.

If you are required to notify other objects when the params change, then you need an Observer Design Pattern implementation. Basically, an object registers with another object as an observer. Then when notified object changes, it notifies the observer. If there are many observers you may need a Mediator object to notify multiple obserevers. The observer objects register witht eh Mediator. The Mediator watches for change in the target object.

Once an object is notified that a target has changed, the target object can be accessed to determine what the change was. Usually, the changed data is not passed around but only the fact that a change occurred is passed.

This is a common problem. For example, when your address changes, the mailing list, order database, department of motor vehicles, etc.. need to know about that.

Get a copy of Design Patterns by Erich Gamma, et. al. though I expect a little Google will bury you in info.
May 4 '10 #2
hype261
207 100+
Even though I have the gang of 4 designs pattern book sometimes I use the website listed below look up design patterns if I don't have the book close by. It is comparable to the book and gives code examples in other languages besides C++ and Small Talk.

http://sourcemaking.com/design_patterns/observer
May 4 '10 #3
kardon33
158 100+
Thanks for the great suggestions, the web site looks really helpful. However I ended up going with a more crude or dirty approach that fits my needs.

Since all modules (each has a class) of my system that stem out from the main class and all have just one function that my main class calls in every time the program loops. I send a pointer of my argument structure in each one of the modules loop function. They then copy it locally, do what they need to do, then copy back their changes to the main classes.

Its gonna work for my current design, definitely not the cleanest approach though.
May 4 '10 #4
Banfa
9,065 Expert Mod 8TB
Erm, does this program work in more than one thread of execution? (i.e. is it multi-threaded or multi-process or multi-tasked?)
May 5 '10 #5
kardon33
158 100+
No, it is not designed to. Well for the most part, there is a bit of code that I run multithread so it doesnt hold up the rest of the program. But that portion of the code is not dependent on any program data that im trying to keep track of.

Would my current design fail if I were to add multi-thread schema to it. I think I see why it would.
May 5 '10 #6
Banfa
9,065 Expert Mod 8TB
Well within each loop function you "copy it locally, do what they need to do, then copy back their changes to the main classes".

If that were multi threaded then presumably the loop functions would be running at the same time and that leaves the possibility of taking a copy in one loop while another loop is writing it back. Any time a multi-thread program allows the possibility of more than one access to a piece of data where at least one access is a write the possibility exists that you will get data corruption which rarely leads to a good end.
May 6 '10 #7
emibt08
25
You could of course make a base class which takes your struct (possibly in the constructor) and derive all other classes that use it from that base class.
The base class would have the struct as a protected member (or private with accessor functions), and you may put there all of your common functions declared as virtual if something in the derived classes needs to be done differently. Eg:

Expand|Select|Wrap|Line Numbers
  1. class Base
  2. {
  3.     Base(params* par) : params_(par) {}
  4.     virtual ~Base() {}
  5.  
  6. protected:
  7.     params* params_;
  8. };
  9.  
  10. class Derived : public Base
  11. {
  12.     Derived(params* par) : Base(par) {}
  13.     void useParams() { /* use params_ */ }
  14. }
  15.  

Then, if you access it with multiple threads, as mentioned, you could do it this way:

Expand|Select|Wrap|Line Numbers
  1. class Base
  2. {
  3.     Base(params* par) : params_(par) {}
  4.  
  5.     params Params()
  6.     {
  7.         lock();
  8.         params p = *params_;
  9.         unlock();
  10.         return p;
  11.     }
  12.  
  13.     void setParams(params* par)
  14.     {
  15.         lock();
  16.         params_ = par;
  17.         unlock();
  18.     }
  19.  
  20.     void lock();
  21.     void unlock();
  22.  
  23. private:
  24.     params* params_;
  25. };
  26.  
where lock/unlock use some locking mechanism, depending on the OS that you're targeting, but possibly a mutex, or R/W lock (CRITICAL_SECTION would be a good choice for Windows)
May 6 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Sasha | last post by:
Hi everyone, I would like to hear your opinions about a design solution I am contemplating. The problem I am following: Write an editor for a data structure that is recursive in nature. In...
4
by: Thomas Paul Diffenbach | last post by:
Can anyone point me to an open source library of /statically allocated/ data structures? I'm writing some code that would benefit from trees, preferably self balancing, but on an embedded system...
1
by: Kenny ODell | last post by:
I am struggling a bit with XML, even as I wade through endless help files and several books. I thought it would be useful to toss my particular needs to the group to see what you think is the best...
6
by: zacks | last post by:
I recently wrote a VB.NET application using VS2005. I needed to be able to build linked lists in memory of items that were represented by a group of variables. For example, Friend Structure foo...
11
by: Macca | last post by:
Hi, I'm writing an application that will pass a large amount of data between classes/functions. In C++ it was more efficient to send a pointer to the object, e.g structure rather than passing...
6
by: Stan R. | last post by:
One more question if I may. From what I've gathered, usually you include the XSLT template right into your main xml doc you're working with, like: <?xml version="1.0" encoding="ISO-8859-1"?>...
2
by: De_Cisse | last post by:
Hi all, I'm working on an application in which I already make some preparations to be able to work in a frontend and backend database, even if this isn't the issue a this moment. I'm using VBA...
0
by: jehugaleahsa | last post by:
On Jun 13, 3:09 pm, "Bob Powell " <b...@spamkillerbobpowell.net> wrote: I apologize for the size. I should have probably put this on a blog or something. I'm not interested in tools. I...
5
by: jc | last post by:
RE: Two Classes with the same Data Structure.. saving code? Inheriting a structure? I have two classes. One in Inherits System.Collections.CollectionBase, the other does not, but they both have...
2
by: jehugaleahsa | last post by:
Hello: I have a bunch of related items that are either parents, children or not directly related to each other. In my case, I have a bunch of database tables joined with foreign keys. Another...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.