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

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 1978
NO***@nospam.com 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(){return m_nSize;};
void SetSize(int size){m_nSize=size;};
......
}
//in different data access class
CMyClass:GetLinkData()
{
CLinkList link;
int s=link.GetSize();
}
CMyclass1:SetSize()
{
CLinkList link;
link.SetSize(5);
}
<NO***@nospam.com> ??? news:fe********************************@4ax.com
???...
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.com> wrote in message
news:fe********************************@4ax.com...
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
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...
6
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...
57
by: Xarky | last post by:
Hi, I am writing a linked list in the following way. struct list { struct list *next; char *mybuff; };
0
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...
8
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...
12
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...
2
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...
12
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...
7
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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...

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.