473,748 Members | 3,585 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

linked list that is shared between many classes?

Don't know if this is the place to post this, but I will give it a go.
:)

I have a linklist class, and I have around 20 other classes that need
to access the same info (the data) that is contained in the linklist
class.

What would be the best way to share access to the data between all the
classes? Should I just have all the classes adopt the linklist class
as a friend, or use a global for the linklist, then access the data
via global calls? Or is there another option I am not seeing?

In case you haven't guessed, I am porting a old C code to C++, and the
C code used tons of globals, but I was thinking there must be a
"nicer" way to do this in C++.

Suggestions/Comments?
Jul 22 '05 #1
3 1988
NO***@nospam.co m wrote:
Don't know if this is the place to post this, but I will give it a go.
:)

I have a linklist class, and I have around 20 other classes that need
to access the same info (the data) that is contained in the linklist
class.

What would be the best way to share access to the data between all the
classes? Should I just have all the classes adopt the linklist class
as a friend, or use a global for the linklist, then access the data
via global calls? Or is there another option I am not seeing?

In case you haven't guessed, I am porting a old C code to C++, and the
C code used tons of globals, but I was thinking there must be a
"nicer" way to do this in C++.

Suggestions/Comments?

You could start by putting the globals in a separate namespace.
Consider dividing them into different namespaces (and different files)
according to who uses them, and you'll end up with a few, distinct modules.

What to do next depends on how much code needs access to each of the
variables. If it's not too much, you could start moving the variables
into implementation files, and your module's interface could include
only methods to access the variables as needed. If a module has too
many clients for this approach, just insert some "using" declarations
and be done with it.

Jul 22 '05 #2
//derive your linklist class
class CLinkList
{
protected:
int m_nSize:
int m_nPoint;
publie:
int GetSize(){retur n m_nSize;};
void SetSize(int size){m_nSize=s ize;};
......
}
//in different data access class
CMyClass:GetLin kData()
{
CLinkList link;
int s=link.GetSize( );
}
CMyclass1:SetSi ze()
{
CLinkList link;
link.SetSize(5) ;
}
<NO***@nospam.c om> ??? news:fe******** *************** *********@4ax.c om
???...
Don't know if this is the place to post this, but I will give it a go.
:)

I have a linklist class, and I have around 20 other classes that need
to access the same info (the data) that is contained in the linklist
class.

What would be the best way to share access to the data between all the
classes? Should I just have all the classes adopt the linklist class
as a friend, or use a global for the linklist, then access the data
via global calls? Or is there another option I am not seeing?

In case you haven't guessed, I am porting a old C code to C++, and the
C code used tons of globals, but I was thinking there must be a
"nicer" way to do this in C++.

Suggestions/Comments?

Jul 22 '05 #3

<NO***@nospam.c om> wrote in message
news:fe******** *************** *********@4ax.c om...
Don't know if this is the place to post this, but I will give it a go.
:)

I have a linklist class, and I have around 20 other classes that need
to access the same info (the data) that is contained in the linklist
class.
Data is not contained in classes, it is contained in instances of classes
(i.e. objects). From your two proposed solutions I think you are confused
about this distinction.

What would be the best way to share access to the data between all the
classes? Should I just have all the classes adopt the linklist class
as a friend,
It is not possible to 'adopt' friendship, you've got it backwards,
friendship is granted by one class to another. In any case friendship is
granted between classes. It has no relevance at all to sharing data between
objects.
or use a global for the linklist, then access the data
via global calls?
Global variables are rarely a good idea.
Or is there another option I am not seeing?

In case you haven't guessed, I am porting a old C code to C++, and the
C code used tons of globals, but I was thinking there must be a
"nicer" way to do this in C++.
I don't think language choice is relevant here. You can have badly designed
programs that use tons of globals in C and C++, similarly you can have well
designed programs in both C and C++. As far as avoiding abuse of global
variables I don't think C++ has any advantages over C.

Suggestions/Comments?


I think the answer is the usual one, aggregate the data into coherent pieces
(call them objects if you like). Create these objects where they are needed
and pass them as parameters to where they are used.

In the case of your linked list, consider passing a reference or a pointer
to the list to the constructor of each object that needs access to the list.
Pointers and references are the way to share data in C or C++.

john
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2327
by: Darryl B | last post by:
I can not get anywhere on this project I'm tryin to do. I'm not expecting any major help with this but any would be appreciated. The assignment is attached. The problem I'm having is trying to set up the class link and tel_list. I set up a class person with strings for name, town, and number. I just don't know how to set up the classes w/ their methods (constructors and operations). I'm stuck on the assignment operator and the add and...
6
3105
by: massimo | last post by:
Hey, I wrote this program which should take the numbers entered and sort them out. It doesn¹t matter what order, if decreasing or increasing. I guess I'm confused in the sorting part. Anyone has any advices?? #include <iostream> using namespace std;
57
4292
by: Xarky | last post by:
Hi, I am writing a linked list in the following way. struct list { struct list *next; char *mybuff; };
0
1485
by: maaveerar_pingpong | last post by:
Hello all, I am using GCC version 3.4.3 on a Linux 2.6 Kernel based machine. I have a library of classes in an partially (incremenally?) built object file, i.e. linked using -Ur option. I link this to a shared library. One of classes in the object library, has an inline fuction which is called from the shared lib as shown below. This inline function All projects compile and link without errors.
8
2079
by: bonk | last post by:
Is it generally OK for an EXE that has MFC linked statically to load an use another DLL wich has MFC linked as shared DLL ? To be more specific: I have an EXE that links a lib. Let's call it mylib.lib. That Lib as well as the EXE have MFC linked statically. Then in a completely different project I have a regular DLL, wich is compiled with the /CLR switch and therefore needs the special CRT libs as
12
3952
by: joshd | last post by:
Hello, Im sorry if this question has been asked before, but I did search before posting and couldnt find an answer to my problem. I have two classes each with corresponding linked lists, list1 and list2, each node within list1 has various data and needs to have a pointer to the corresponding node in list2, but I cant figure out how to do this. Could someone explain what I might be missing, or maybe point me in the direction of a good...
2
5617
by: Subodh | last post by:
Hi All, I want to use a C++ API in a static lib that returns a linked List in C# I am planning to P/Invoke to perform the Interop, I would like to know which way will be better to interop a Linked list to C# in terms of performance, I have came across following methods I am creating a flat DLL to export the static lib functionality.
12
4049
by: kalyan | last post by:
Hi, I am using Linux + SysV Shared memory (sorry, but my question is all about offset + pointers and not about linux/IPC) and hence use offset's instead on pointers to store the linked list in the shared memory. I run fedora 9 and gcc 4.2. I am able to insert values in to the list, remove values from the list, but the problem is in traversing the list. Atlease one or 2 list nodes disappear when traversing from the base of the list or...
7
5772
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it compiled no problem. But I can't find the what the problem is with templates? Please help. The main is in test-linked-list.cpp. There are two template classes. One is List1, the other one is ListNode. The codes are below: // test-linked-list.cpp :...
0
9530
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9363
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9238
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8237
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4593
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.