linked list that is shared between many classes?

J

james

//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);
}
 
N

NOSPM

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?
 
J

Jeff Schwab

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.
 
J

John Harrison

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top