How to pass the address of private data through main() function?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tushu
    New Member
    • May 2015
    • 9

    #1

    How to pass the address of private data through main() function?

    printing elements of Linked list in reverse order
    Code:
    #include "stdafx.h"
    #include<iostream>
    #include<conio.h>
    using namespace::std;
    struct node
    {
        int data;
        node* next;
    };
    class linklist
    {
        node* head;
    public:
        linklist()
        {
            head = NULL;
        }
        void create_list(int d);
        void reverse_print_list(node*);
    }
    
    void reverse_print_list(node* p)
    {
        if (p == NULL)
            return;
        reverse_print_list(p->next);
        cout << endl << p->data << " ";
    }
    
    int main()
    {
        linklist l1;
        l1.create_list(10);
        l1.create_list(20);
        l1.create_list(30);
        l1.create_list(40);
        l1.create_list(50);
        l1.print_list(& head);  // Not allowed , gives compilation error 
        _getch();
        return 0;
    }
    Here I want to pass the address of "head". But it is not accessible outside class as it is private data
    How to solve this problem ???
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Why would you want to pass the address of a private data member??

    Doing so trashes the entire reason for having a class in the first place because only the linklist class knows how to manage this data.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      Consider instead adding a method to the class that does whatever you were planning to do with the private member information.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Maybe make the reverse_print_l ist function a member function of linklist.

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          Reverse-traversal might be a reasonable capability for your class, then an external reverse-print function could use that to accomplish what you want.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            As long as it does the reverse without returning the address of the head of the list. Once the address of the head of the list escapes, the security of the list is compromised making re-use of this code in another program zero.


            If you use an external reverse traverse, then, perhaps, the linklist has a "get previous data" function. This would return a COPY of the data and not the address of the previous node.

            The node address can't escape for the same reason as the head address. The list structure must be managed by the linklist class and the node managed by the node class. (this also means the linklist can't change the data in the node. linklist would need to call a node method to do that).

            Comment

            • tushu
              New Member
              • May 2015
              • 9

              #7
              Thank you friends for your help. problem was solved by doing this :
              reverse_print_l ist () { reverse_print (head) ; }

              Comment

              Working...