473,395 Members | 1,504 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,395 software developers and data experts.

Friend class - private member variable

How do we access the private member variable outside the class with out using friend ?
Mar 27 '07 #1
5 2275
horace1
1,510 Expert 1GB
How do we access the private member variable outside the class with out using friend ?
via get() and set() methods?
Mar 27 '07 #2
sanctus
84
via get() and set() methods?
Or just by definig any function in the class, so it will have access to the private member:
Expand|Select|Wrap|Line Numbers
  1. class exple{
  2. int i;
  3. public:
  4. (...)
  5. int fct(int x){x=i;cout<<x;}
  6. };
  7.  
Or something like that
Mar 27 '07 #3
You can try something like this,

Expand|Select|Wrap|Line Numbers
  1. class X
  2. {
  3.     int i;
  4.  
  5. public:
  6.     X():i(10){};
  7.  
  8.     int* fn()
  9.     {
  10.         return &i;
  11.     }
  12. };
  13.  
  14. void main()
  15. {
  16.     X x;
  17.     int* pI = x.fn();
  18.     cout << *pI;
  19.  
  20.  
  21. }
Mar 30 '07 #4
Thanks guys for the response
Apr 23 '07 #5
weaknessforcats
9,208 Expert Mod 8TB
Of course, providing access to the private member breaks encapsulation. That means you can no longer rely on the member functions to protect the values in the data member. So, if the value get screwed up your list of suspects exopands from just the member functions to all the function in program that can touch that data member directly or indirectly.
Apr 23 '07 #6

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

Similar topics

5
by: Avinash | last post by:
Hi, Why Overloaded operator cannot be a friend ? Thanking You. Avinash
12
by: Bryan Parkoff | last post by:
CMain Class is the base class that is initialized in main function. CA Class is the base class that is initialized in CMain::CMain(). CMain Class is always public while CA Class is always...
5
by: Srini nandiraju | last post by:
Hi folks, I am trying to implement a friend class and the following is what I did. Please scroll down. /***************** CODE **********************************/ class BKP { private: int...
2
by: wongjoekmeu | last post by:
Hello all, I have the following problem, maybe someone can help me out. I have a template class A which has a protected member Aint. Now I have template class B which has as private member an...
15
by: Samee Zahur | last post by:
Question: How do friend functions and static member functions differ in terms of functionality? I mean, neither necessarily needs an object of the class to be created before they are called and...
1
by: yancheng.cheok | last post by:
currently, i have a private function in cat named privateFun. i would like to have this function "private" to all except dog's action member function. by using the following approach, all the...
6
by: Joseph Turian | last post by:
I am having difficulty defining a friend function because of a #include cycle: ============ obj.H ================ #ifndef _obj_ #define _obj_ #include "cont.H" class cont;
6
by: Sunny | last post by:
Hi All, In C++ , if a function or class is declared friend, then where is that information stored? How does friend function get access to private data of a Class? Thanks
2
by: Immortal Nephi | last post by:
I design a class for general purpose. I do not allow a client to read or modify interface and implemention. I allow them to write a new non- member function outside of class' interface and...
1
by: Immortal_Nephi | last post by:
I want to show you an example how two classes can have relationship instead of an inheritance. The inheritance is not an option if you want to define two classes. The relationship between two...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.