Hi All,
One of my friend asked this question on C++
Hi,
Can u give an answer to this :
We have the .h files for standard library. Consider any class (such as
ostream) declared in the .h files. Can I edit the .h file to declare my
own function as friend inside the class declaration and then gain
access to private data members of ostream? Will the compiler allow
this?
If yes, that means we can gain access to private data members to even
3rd party libraries. How much useful/destructive that is, is a
different issue.
--Ankit
<<<
We all know that this will not be possible, beacuse the information
about the private variables will not be available in library (.lib or
..a files) and the linker will complain.
Can any one elaborate on this issue?
Thanks,
Chetan Raj
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ] 39 2071
Chetan Raj wrote: Hi All,
One of my friend asked this question on C++ Hi, Can u give an answer to this :
We have the .h files for standard library. Consider any class (such as ostream) declared in the .h files. Can I edit the .h file to declare my own function as friend inside the class declaration and then gain access to private data members of ostream? Will the compiler allow this?
More than likely it will work. However it is not required to work by
the standard. The standard requires that the definition of the class
remains identical in all compilation units that use it, so this is a
clear violation of that requirement. However, I don't believe I've
witnessed a compiler ever enforce that requirement. If yes, that means we can gain access to private data members to even 3rd party libraries. How much useful/destructive that is, is a different issue.
--Ankit
<<<
We all know that this will not be possible, beacuse the information about the private variables will not be available in library (.lib or .a files) and the linker will complain.
There is nothing stopping you from resolving those conflicts in another
library/object file. However, you have a problem when a new revision of
the library comes out, it may be that the interface is changed such that
your code may need to be dramatically changed - not fun. Can any one elaborate on this issue?
If anyone on my dev team were to do this, they would be severly chastized.
Let's just say no and be done with it. If a class wants you to look at
its private parts, they will be protected and you can use inheritance.
Some standard libraries support non-standard functionality that allow
you to do just that. For the iostreams, you can extend them with
inheritance or without inheritance through streambufs, locales,
callbacks, and the xalloc/iword/pword mechanism.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]
* Chetan Raj: One of my friend asked this question on C++
Hi, Can u give an answer to this :
We have the .h files for standard library. Consider any class (such as ostream) declared in the .h files. Can I edit the .h file to declare my own function as friend inside the class declaration and then gain access to private data members of ostream? Will the compiler allow this?
If yes, that means we can gain access to private data members to even 3rd party libraries. How much useful/destructive that is, is a different issue.
--Ankit <<<
We all know that this will not be possible, beacuse the information about the private variables will not be available in library (.lib or .a files) and the linker will complain.
Nope, we don't all know that: you can access the private variables all you
want.
Instead of editing the header files you can duplicate the declarations.
Or whatever, including pointer hacking.
Can any one elaborate on this issue?
It's very simple. 'private' is protection against _inadvertent_ access of
private things, and helps to avoid name collisions. It's not a security
measure. Anyone can access anything if they're really determined.
'private' is not protecting against hacking: it is a design tool.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]
May be...
when u edit the interface, you just change it. The implementation is in
..a or .so files ( whatever). And you can't change it.
And i think, those .h'ss are read only and to be used when u write the
client code.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]
Access control is a language mechanism for organizing large scale software
construction. The sole purpose of access control is to limit the number of
elements that can be affected from code change, i.e. if you decide to change
the implementation of a class, as long as you keep the public interface
unchanged, the only things affected by the change are the member functions
of that class and friend classes.
Yes you can change the header and get access to private member of library
code. But at what price? You don't get privilege doing that, and most
importantly, if you write 10000 lines of code that accesses to the private
member of a class, you'd hope that the class doesn't get updated because
otherwise you will have 10000 lines of code to review.
ben
"Chetan Raj" <hi*****@gmail. com> wrote in message
news:11******** *************@o 13g2000cwo.goog legroups.com...
[excess quoting deleted -- mod] We have the .h files for standard library. Consider any class (such as ostream) declared in the .h files. Can I edit the .h file to declare my own function as friend inside the class declaration and then gain access to private data members of ostream? Will the compiler allow this?
If yes, that means we can gain access to private data members to even 3rd party libraries. How much useful/destructive that is, is a different issue.
--Ankit
<<<
We all know that this will not be possible, beacuse the information about the private variables will not be available in library (.lib or .a files) and the linker will complain.
Can any one elaborate on this issue?
[excess quoting deleted --mod]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]
Chetan Raj wrote: We have the .h files for standard library. Consider any class (such
as ostream) declared in the .h files. Can I edit the .h file to declare
my own function as friend inside the class declaration and then gain access to private data members of ostream? Will the compiler allow this?
Without going into the merits of why you should do this, the answer
would be: Yes, you can edit the .h file and add a friend function
declaration
and access the private members of the class from that function. The
compiler will not complain.
If yes, that means we can gain access to private data members to even 3rd party libraries. How much useful/destructive that is, is a different issue.
Yes, but it is a very basic and important issue. Note that C++ access
specifiers are for data encapsulation and are not for preventing
intentional misuse.
We all know that this will not be possible, beacuse the information about the private variables will not be available in library (.lib or .a files) and the linker will complain.
If you change an existing class's definition in the header file in such
a way that it becomes incompatible with the one used by the library,
then you could be hit by any number of compilation errors and run-time
errors.
However, adding a friend declaration as mentioned above should not
cause the linker to complain.
Regards,
Murali
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]
Chetan Raj wrote: We have the .h files for standard library. Consider any class (such
as ostream) declared in the .h files. Can I edit the .h file to declare
my own function as friend inside the class declaration and then gain access to private data members of ostream? Will the compiler allow this?
The standard doesn't say what will happen here. However, I've never
used a compiler where this wouldn't work (though I've heard of some
where some or all of the standard headers aren't literally text
files -- obviously it wouldn't work there).
If yes, that means we can gain access to private data members to even 3rd party libraries. How much useful/destructive that is, is a different issue.
Very destructive! Please don't do this unless you've found a bug in
the library that can be fixed just by editing header files. (Most
template code falls into this category.) Even then, please check for
official "patches" instead of fixing it yourself!
We all know that this will not be possible, beacuse the information about the private variables will not be available in library (.lib or .a files) and the linker will complain.
AFAIK, most compilers don't carry public/private information in object
code -- they rely on the One Definition Rule (ODR), and assume that
the header files have NOT been altered. Search the FAQ for "ODR".
Please don't literally edit the header files directly -- you'll find
it hard to get them "back to normal" when you come back to your senses.
(You might have to re-install the entire compiler.) Instead, use a
trick like this:
// Define SANITY when you have come to your senses
//#define SANITY
#if !defined(SANITY )
#define class struct
#define private public
#define protected public
#endif
#include <whatever>
// You now have access to all private members of ALL classes
Obviously UB (Undefined Behavior) is going to be different from one
compiler to the next. Theoretically this type of playing could affect
the way that data members are packed into a structure. But I think
that on many compilers this will do exactly what you probably expect!
So you can do this for toy programs. But, like any other UB, you
can assume that it's going to seem to "work" until it matters most --
probably the day after you've burned CD copies of your finished
programs and started shipping them to paying customers -- and then
it will do something nasty. Therefore you should avoid doing this
in production code!
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]
Chetan Raj wrote: Can I edit the .h file to declare my own function as friend inside the class declaration and then gain access to private data members of ostream? Will the compiler allow this?
That's a quality of implementation issue. By having two different
definitions of a class in the same program, you are violating the
ODR (one-definition rule) and that causes undefined behavior.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]
In article <11************ *********@o13g2 000cwo.googlegr oups.com>,
Chetan Raj <hi*****@gmail. com> writes Hi All,
One of my friend asked this question on C++
Hi, Can u give an answer to this :
We have the .h files for standard library. Consider any class (such as ostream) declared in the .h files. Can I edit the .h file to declare my own function as friend inside the class declaration and then gain access to private data members of ostream? Will the compiler allow this?
If yes, that means we can gain access to private data members to even 3rd party libraries. How much useful/destructive that is, is a different issue.
1) There are no .h files in the c++ Standard (other than those inherited
from C, which do not have any uses of private in them)
2) Any time a programmer alters or inserts anything to namespace std
they need to check that it is one of the very limited cases where that
is permitted.
3) private is not designed to prevent theft or invasion only to help
detect accident.
4) If you really want to steal access just try:
#define private public
Of course the consequences are entirely undefined.
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ] This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: jaysonsch |
last post by:
Hello!
I am having some problems with a database query that I am trying to do.
I am trying to develop a way to search a database for an entry and
then edit the existing values. Upon submit, the new values are updated
in all corresponding tables (the function of the pages in question).
However, on the page that does the DB update, I also want to do some
checks on the data before performing the update.
Now, the problem that I am...
|
by: shank |
last post by:
1) I'm getting this error: Syntax error (missing operator) in query
expression on the below statement. Can I get some advice.
2) I searched ASPFAQ and came up blank. Where can find the "rules" for when
and how to use single quotes and double quotes in ASP?
thanks!
----------------------
SQL = SQL & "WHERE '" &
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE("GenKTitles.
|
by: netpurpose |
last post by:
I need to extract data from this table to find the lowest prices of
each product as of today. The product will be listed/grouped by the
name only, discarding the product code - I use SUBSTRING(ProductName,
1, CHARINDEX('(', ProductName)-2).
I can get this result, but I had to use several views (totally
inefficient). I think this can be done in one efficient/fast query,
but I can't think of one.
In the case that one query is not...
|
by: Harvey |
last post by:
Hi,
I try to write an asp query form that lets client search any text-string and
display all pages in my web server that contain the text. I have IIS 6.0 on a
server 2003. The MSDN site says there is a sample file called Ixtrasp.asp,
but I could not find it in my system although I installed indexing service. I
followed the steps in MSDN site to create a basic .asp query form (too long
to post it here), but it always displays:
No...
|
by: Diamondback |
last post by:
I have two tables, WIDGETS and VERSIONS. The WIDGETS table has
descriptive information about the widgets while the VERSIONS table
contains IDs relating to different iterations of those widgets over the
years. The idea is that from any widget in the database you can look
forward and backward to see different versions of the widget through
the years. Here are the tables:
WIDGETS
widget_id
name
| |
by: Dave Thomas |
last post by:
If I have a table set up like this:
Name | VARCHAR
Email | VARCHAR
Age | TINYINT | NULL (Default: NULL)
And I want the user to enter his or her name, email, and age - but AGE
is optional.
My insert would look something like:
|
by: starace |
last post by:
I have designed a form that has 5 different list boxes where the
selections within each are used as criteria in building a dynamic
query. Some boxes are set for multiple selections but these list boxes
do not necessarily need to have a selection made to be used in the
dynamic query.
In essence the form can have selections made in all or none of its list
boxes to form the dynamic query
I am looking to get some feedback in reference to...
|
by: jjturon |
last post by:
Can anyone help me??
I am trying to pass a Select Query variable to a table using Dlookup
and return the value to same select query but to another field.
Ex.
SalesManID SalesManName
AT Alan Time
|
by: Stan |
last post by:
I am using MS Office Access 2003 (11.5614). My basic question is can
I run a query of a query datasheet. I want to use more that one
criteria and can not get that query to work. I thought I might be
able to accomplish the results in two steps by using two queries. If
this is possible how can I do it?
Thank you,
Stan Hanna
|
by: jsacrey |
last post by:
Hey everybody, got a secnario for ya that I need a bit of help with.
Access 97 using linked tables from an SQL Server 2000 machine.
I've created a simple query using two tables joined by one field
between them. The join field in both tables are indexed and I'm
selecting 1 field from each table to lookup. The Access query is
taking more than 60 second to retrieve 1 record and if I execute the
same query within the Query Analyzer, it...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |