to do this.

A pointer gets created, say

Head

Would it be better to do this

Head = new Node;
Head = NULL;

or is it better to do this.

Head = NULL;
Head = new Node;

The second. The first will cause a memory leek. Although in the code you posted you don't need Head = NULL; because it just changes that pointer to whatever is returned by new.

if want the node clear before you get a new one

Head = NULL;
//or
Head = 0;
Node Head = new Node;

this will make sure it is empty

if want the node clear before you get a new one

Head = NULL;
//or
Head = 0;
Node Head = new Node;

this will make sure it is empty

You are just wasting CPU time doing that. A pointer does not have to be set to 0 (or NULL) before calling new, or assigning it to some other address. There may be other legitimate reasons for doing it, but that is not one of them.

The second. The first will cause a memory leek. Although in the code you posted you don't need Head = NULL; because it just changes that pointer to whatever is returned by new.

The Code I posted in the other thread?

I'm kind of lost as to what you said.
What you mean I don't really need it?

The first is not good, and the second is also a bad one: Head = NULL; Why should you set the pointer to NULL right after the declaration? It's automatically done (If you declare a pointer, it's automatically pointed to the first memory address: NULL) ...

Ancient Draogn said the first one causes memory leak.
But the second one isn't bad.
Now you're saying it's bad?

Doesn't the pointer, once declare it declares to some random memory?

Ancient dragon are you saying this?
That the head shouldn't be NULL?

struct Fast_Food_Business
	{
	   string name;  
                 Fast_Food_Business* head;
                Fast_Food_business* tail;
          
                         };

      struct cash_registers{
         Fast_Food_Business * regist;
         cash_registers * r; 

	}
                         
        struct Customer
        { 
    	   int n;
    	   Customer * h ; // head
    	   Customer *t; // tail
           Fast_Food_Business*next;
			};

     bool menu (const char& number)
     {
      return number== '1' ||      number== '2';
   	
    				   }
    				   
     bool sub_menu (const char& alphabet)
     {
        return alphabet== 'a' ||    alphabet== 'b' ||
	       alphabet== 'c' ||    alphabet== 'd' || 
                                   }

     int main()
   {    
    int Restaurant= 0;

    cout<<"How many Restaurants?"<<endl;
    cin>>Restaurants;
    
    Fast_Food_Restaurant* aRestaurant= new Fast_Food_Restaurant;
    aRestaurant-> t = NULL;  // linked list tail

    for(int i = 1; i < Resturants; i++)
    {	
        aRestaurant -> n = NULL;
        // now add to linked list
        if( aResturant -> h== NULL)  
        {
          aResturant-> h = aRestaurant-> t= aRestaurant;
                                      }
        else
        {
         // add new node to end of linked list
         aRestaurant -> t= aRestaurant-> n = aResturant;
         aResturant -> t= aResturant;
                                       }

    
                                       }

int customer = 0;
cout " how many customers";
cin>>customer;
   
      for (int i=0; i<customer; i++)
  { 
     char choice; 
     cout<<"What would be your choice?";
     cin>> choice;

     if( menu(choice))
    {   
        if (menu== '1')
        {

	cash_register * cr = new cash_register;
        cr = NULL;

        if (CR== NULL)
        {
            aResturant = CR; }	
                
  
  cin.get();
  return 0;     
}

I took away

aRestaurant - h = NULL;

delete line 48.

That loop at lines 46-59 does not create a linked list of structures. It doesn't do anything at all.

Lines 43 and 44 are ok. That will be the head of the linked list and you don't want to change that pointer -- ever.

Now on line 48 you need to allocate a new pointer then add it to the tail of the linked list. That is what you have not done.

for(int i = 1; i < Resturants; i++)
{	
    Fast_Food_Restaurant* NewNode = new Fast_Food_Restaurant;
    NewNode->next = NULL;
        // now add to linked list
        if( aResturant -> head == NULL)  
        {
          aResturant-> head = aRestaurant-> tail = NewNode;
        }
        else
        {
         // add new node to end of linked list
         aRestaurant -> tail ->next = NewNode;
         aResturant -> t= NewNode;
    }
}

Thank You

I want to see if it works though, so I need to get the next problem I'm having working. So I can tell if does create a linked list.

I want to have to have that cash register point to a particular restaurant #, and then in the end the user can see which restaurant makes more.


I think my restaurant numbering them, like if the user wanted to access queue3/restaurant 3

I think it should be a counter and then when that counters reaches that number the user inputs, then linked list/queue that was just created the user can use that?

Not sure if this is the right sense of thinking on how to do this.

And the cash register popinting to the restaurant. Overload the assignment operator? But do I put that in all the structs, or just one struct, the struct that the pointer on the Left Hand Side belong to?

And I tried that before, but that didn't really go anywhere.

PointerA operator = (const& PointerA * C, const& PointerA *D)
{
    PointerA* A = C;
    C = D;
    return C;
}

in the other thread, you said something about swapping.
That's not what the assignment operator does though right?

The left becomes the right. But I'm not sure if this code is done properly.

Ancient Draogn said the first one causes memory leak.
But the second one isn't bad.
Now you're saying it's bad?

Doesn't the pointer, once declare it declares to some random memory?

The first one is definitely causing a memory leak (you declare a pointer, assign newly allocated memory to it and after that you're just setting the pointer to NULL, which means it doesn't point anymore to the previously assigned memory :))

The second one is syntactically not wrong, but after a pointer declaration the pointer is automatically set to NULL , so there's no need for Head = NULL; , it's just a line which causes overhead ...

PointerA operator = (const& PointerA * C, const& PointerA *D)
{
    C = D;
    return C;
}

I think I meant this.


or

PointerA operator = (const& PointerA * C, const& PointerA *D)
{
    PointerA* C;
    PointerA* D;
    C = D;
    return C;
}

Also, do I need to put struct before PointerA?

PointerA operator = (const& PointerA * C, const& PointerA *D)
{
    C = D;
    return C;
}

I think I meant this.


or

PointerA operator = (const& PointerA * C, const& PointerA *D)
{
    PointerA* C;
    PointerA* D;
    C = D;
    return C;
}

Also, do I need to put struct before PointerA?

Why are you making it so complicated? Just pass the struct by reference :)

you mean

PointerA operator = (const& struct PointerA * C, const& struct PointerA *D)
{

    C = D;
    return C;
}

No, take a look at this line: PointerA operator = ([B]const <datatype?>&[/B] PointerA * C, const& PointerA *D) according to my C++ knowledge this won't compile, you also have to specify what type the constant is e.g. a constant integer is declared as const int , not just as const ... C = D; what is it actually? You've to return a reference to the object ...

Take a look at this example :)

revised

PointerA operator = (const& struct PointerA * C, const& struct PointerA *D)
{

    C = D;
    return C;
}

With the code, i get this compiler message.

PointerA& PointerA::operator=(const PointerA&, const PointerA&)' must take exactly one argument

the { gets highlighted

In member function `PointerA& PointerA::operator=(const PointerA&, const PointerA&)':

C=D gets high lighted

no match for 'operator=' in 'C = D'
candidates are: PointerA& PointerA::operator=(PointerA&)

the return C gets high lighted

invalid initialization of reference of type 'PointerA&' from expression of type 'const PointerA'

Did you actually read my post thoroughly? Did I say that you had to use the struct keyword ?
Did you take a look at the example ? (If you don't understand something then just ask:))

^
oh that was a mistake, that struct shouldn't have been there.
I just copied and paste on the post before, and just moved the & around.

I forgot it had the struct in it.

In that link you just posted.

I dun get what "this" is.
it says if(this==rhs
what's this?

In that link you just posted.

I dun get what "this" is.
it says if(this==rhs
what's this?

It's just a check whether the object is being assigned to itself or not :)

The second one is syntactically not wrong, but after a pointer declaration the pointer is automatically set to NULL , ..

That is incorrect. The pointer is assigned whatever random value is at that memory location. For example char *ptr; the address of ptr is whatever is at that memory location on the stack (assuming its not a global variable). It is never auto set to NULL by the compiler in either C or C++ languages.

So overloading the assignment operator only takes one argument?

Maybe, it's staying up at for a couple of days.
But I don't see something that I can work with in that example.

it checks if it's assigned to itself or not?
so the if statement says if its assigned to itself
it returns itself?

that doesn't make sense to me ><
at least not in my state of mind, right now.

PointerA operator = (const PointerA& *A)
{     
            if(*this == *A)
                  return *this;
}

Should that me my definition for overload the assignment op.

>>That is incorrect. The pointer is assigned whatever random value is at that
>>memory location. For example char *ptr; the address of ptr is whatever is at that
>>memory location on the stack (assuming its not a global variable). It is never
>>auto set to NULL by the compiler in either C or C++ languages.

I was just about to point out that to the OP and tux.
This might now confuse OP as Tux said "that explicitly initializing a pointer to 0 is a overhead because it is done by default" which is actually wrong.
The overhead is because you can construct the pointer pointing to a new location by directly by initializing it to the result of a new operation:

//version 1// BAD
int* P;
P=new int;
/
//version 2// Good
int* P= new int;
//This is good as you are saving the overhead of assignment.
//As there is no need of assignment, cause you have initialized the P already

Not to mention, assignment and initialization are two different things

Comments
Everywhere siddhant is looking for confusion :)

I think I realize what I'm asking.

I'm just asking how to make my cash register node (which is from a different struct) get inserted to the restaurant pointer linked list(Which is from another struct).

Do I need to overload the assignment operator? because the compiler is saying.

cash register can't convert to fast_food_business

struct Fast_Food_Business
{
    string name;  
    struct cash_registers* RegHead;
    struct cash_registers* RegTail;
};

struct cash_registers
{
    int register_number;
    float total_sales_this_register;
    struct Customer* CustHead ; 
    struct Customer* CustTail;
};
                         
struct Customer
{ 
    int n;
    struct foodItem* foodHead;
    struct foodItem* foodTail;

};

struct foodItem
{
   string food_name;
   int quantity;
   float price;
};

int main()
{
    struct Fast_Food_Business* BusinessHead = new Fast_Food_Business;
  
    BusinessHead->RegHead = new struct cash_registers;
   
    BusinessHead->RegHead->CustHead = new struct Customer;
}

You still have a big problem with those structures. The structures would be allocated something like the above. Note: this is not complete, you still have to add the loops to allocate however many nodes in the linked lists that you want.

Is that code in main, the code that creates a link list of a linked list?

And what's the big problem ?

Is that code in main, the code that creates a link list of a linked list?

yes

And what's the big problem ?

Compare the structures I posted with the ones you posted and you will see the big problem in your structures.

I think i should get some sleep, but I can't T_T
Alright, i'm trying to understand the three line of code that's in the main.

First line I get.
there's a pointer that gets created called BusinessHead, adn that's pointing to a new Fast_food_business node.

second line says,

that business head head pointer is pointing to a new cash register node.

third line.

I'm kind of lost on that one.

How can business head be pointing to a CustHead when it's pointing to a new cash register?

Or is it because it has something to do with regHead is a cash_register pointer data type?

This article has been dead for over six months. Start a new discussion instead.