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

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

9
printing elements of Linked list in reverse order
Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. #include<iostream>
  3. #include<conio.h>
  4. using namespace::std;
  5. struct node
  6. {
  7.     int data;
  8.     node* next;
  9. };
  10. class linklist
  11. {
  12.     node* head;
  13. public:
  14.     linklist()
  15.     {
  16.         head = NULL;
  17.     }
  18.     void create_list(int d);
  19.     void reverse_print_list(node*);
  20. }
  21.  
  22. void reverse_print_list(node* p)
  23. {
  24.     if (p == NULL)
  25.         return;
  26.     reverse_print_list(p->next);
  27.     cout << endl << p->data << " ";
  28. }
  29.  
  30. int main()
  31. {
  32.     linklist l1;
  33.     l1.create_list(10);
  34.     l1.create_list(20);
  35.     l1.create_list(30);
  36.     l1.create_list(40);
  37.     l1.create_list(50);
  38.     l1.print_list(& head);  // Not allowed , gives compilation error 
  39.     _getch();
  40.     return 0;
  41. }
  42.  
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 ???
May 13 '15 #1

✓ answered by donbock

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

6 1505
weaknessforcats
9,208 Expert Mod 8TB
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.
May 13 '15 #2
donbock
2,426 Expert 2GB
Consider instead adding a method to the class that does whatever you were planning to do with the private member information.
May 13 '15 #3
weaknessforcats
9,208 Expert Mod 8TB
Maybe make the reverse_print_list function a member function of linklist.
May 14 '15 #4
donbock
2,426 Expert 2GB
Reverse-traversal might be a reasonable capability for your class, then an external reverse-print function could use that to accomplish what you want.
May 14 '15 #5
weaknessforcats
9,208 Expert Mod 8TB
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).
May 14 '15 #6
tushu
9
Thank you friends for your help. problem was solved by doing this :
reverse_print_list () { reverse_print (head) ; }
May 15 '15 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Adrian | last post by:
I am trying to pass the address of a C++ function into a Fortran routine to enable the Fortran routine to call this C++ function. I have to do it this way as our build process does not allow...
89
by: Sweety | last post by:
hi, Is main function address is 657. its show in all compiler. try it & say why? bye,
1
by: Jaspreet | last post by:
I am working on a code which is similar to the following snippet: #include <iostream> using namespace std; class def; class abc {
6
by: zfareed | last post by:
Is it possible to print an array that is declared from a class type that also contains private data members? I have declared an array in the main function and then set the values for the array...
2
by: pascal.zschumme | last post by:
hello folks My problem is that the following code using something very very difficult technique :D fails to compile on MSVC8: <code> // main.cpp // // the error is: // main.cpp(8) : fatal...
28
by: Bill | last post by:
Hello All, I am trying to pass a struct to a function. How would that best be accomplished? Thanks, Bill
1
by: vinothg | last post by:
Guys, I just wanna know how compiler diffrentiates between a ordinary function and member function. I know that compiler will secretly pass the this pointer to member functions. Eg : look at...
2
by: vinothg | last post by:
Hi All, I just wanna know how compiler diffrentiates between a ordinary function and member function. I know that compiler will secretly pass the this pointer to member functions. Eg : look at...
4
by: S. | last post by:
Hi all, I have the requirement that I must pass-by-reference to my function addStudent() and getAge() functions where my getAge() function is within the addStudent() function. I am able to...
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.