April 26, 2024, 08:22:41 PM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Where to declare linked lists

Started by Bruce Peaslee, October 07, 2006, 05:52:31 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Bruce Peaslee

It appears that if I try to declare a linked list as global (outside of a routine), it doesn't work.
Bruce Peaslee
"Born too loose."
iTired (There's a nap for that.)
Well, I headed for Las Vegas
Only made it out to Needles

Ionic Wind Support Team

The linked list is a class, and global classes don't currently get constructed.

If you just want to use it from procedural programming then use a global pointer to the list and create it dynamically.
Ionic Wind Support Team

Bruce Peaslee

Quote from: Paul Turley on October 07, 2006, 06:25:40 PM
The linked list is a class, and global classes don't currently get constructed.

You know, I did read that somewhere, but didn't know enough at the time to grasp its meaning.ÂÃ,  ;)

I'll rearrange the code a bit.
Bruce Peaslee
"Born too loose."
iTired (There's a nap for that.)
Well, I headed for Las Vegas
Only made it out to Needles

Bruce Peaslee

This is what I tried:


class SaleDataDialog cDialog
{
ÂÃ,  ÂÃ, CPointerList m_SaleList;
ÂÃ,  ÂÃ, declare SetLinkedList(CPointerList List)
ÂÃ,  ÂÃ, ...
}

SaleDataDialog::SetLinkedList(CPointerList List) // list is set up in another class instance
{
ÂÃ,  ÂÃ, m_SaleList = List;
ÂÃ,  ÂÃ, return;
}



This works to a point. The list data comes through and can be displayed, but when a try to exit the program:

  • all the windows close
  • the program continues to run in the background
  • it consumes about 50% of CPU time

Any ideas?
Bruce Peaslee
"Born too loose."
iTired (There's a nap for that.)
Well, I headed for Las Vegas
Only made it out to Needles

Ionic Wind Support Team

Use pointers man.  You can't arbitrarily copy once class to another.  Sure it will copy the member variables but the variables are valid only for the class instance that they were created in.  In other words you don't have one linked lists but two when you do that.

So here is how you handle it.


class SaleDataDialog cDialog
{
   CPointerList *m_pSaleList;
   declare SetLinkedList(CPointerList *List)
   ...
}

SaleDataDialog::SetLinkedList(CPointerList *List) // list is set up in another class instance
{
   m_pSaleList = List;
   return;
}


And then just use the -> operator instead of the .

Paul.
Ionic Wind Support Team

Bruce Peaslee

Thanks. That works.

In general I'm trying to avoid globals so I thought I would just pass the class. It almost worked except the program kept crashing.ÂÃ,  :D
Bruce Peaslee
"Born too loose."
iTired (There's a nap for that.)
Well, I headed for Las Vegas
Only made it out to Needles

Ionic Wind Support Team

You're welcome.  Just a bit more info. 

When copying a class it acts just like a structure would, copying the contents of each member variable to the new class.  Which is where the problem lies since a member variable might contain a pointer to memory that was allocated for that class instance.  So both class variables end up pointing to the same block of memory.  And when one goes out of scope it might delete that memory leaving the other one hanging.

What we are going to have is a copy constructor soon.  Which allows you to create a special method to handle when a class is copied from another.  Lets say you had a class that allocated memory when it was created, and had a member variable called m_nStatus. The copy constructor might look like this:

CMyClass::Opr_EQ(CMyClass *copy)
{
Create();
m_nStatus = copy->GetStatus();
}

So when you do something like so:

CMyClass cNew;
cNew = cOld;

Instead of just blindly copying the member variables the compiler will check to see if you have a copy constructor first and insert the call:

cNew.Opr_EQ(&cOld);

Have fun,
Paul.

Ionic Wind Support Team

Parker

Instead of adding more specific-purpose identifiers like with constructor and destructor, I would prefer to have a new keyword and/or syntax, like "operator =" in C++. It just looks better to me than giving specific identifiers a different purpose in some situations (easter eggs ;))

Ionic Wind Support Team

Depends on whether or not YACC pukes on it.  Hard enough to get the equal sign to work for both an assignment and boolean operator.

Plus I was just explaining what it does, it wasn't a schematic of anything.
Ionic Wind Support Team

Parker

If OPERATOR was a keyword I wouldn't imagine it would have a hard time with it, but I don't know much about how yacc handles things.

(just thinking out loud)
Even if OPERATOR isn't a keyword, when else can you write ID :: ID = ...? It would make sense that it would accept that, but I'm not sure because I haven't used yacc much. Hand written parsers are fun because you can write lots and lots of context-sensitivity into them ;) But then for upcoming operator overloading, OPERATOR should be a keyword anyway so that shouldn't be a problem...