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

Method access question

Hi all,
How does this work?

class a{
public:
int geta(){
return 2;
}
};
int main(){
a *ptr = 0;
cout<<"Value :"<<ptr->geta();
}

this prints
Value: 100

I expected a runtime error. but this worked just fine. How?

Regards
area

Sep 8 '06 #1
2 1404
<en******@gmail.comwrote in message
news:11*********************@h48g2000cwc.googlegro ups.com...
: Hi all,
: How does this work?
:
: class a{
: public:
: int geta(){
: return 2;
: }
: };
:
:
: int main(){
: a *ptr = 0;
: cout<<"Value :"<<ptr->geta();
: }
:
: this prints
: Value: 100
:
: I expected a runtime error. but this worked just fine. How?
"worked just fine" ?? I assume there is a typo in your
post then (2 vs 100).

Anyway:
- The C++ standard does not require any checks
for NULL or invalid pointers during dereferencing.
What is specified in that case is "undefined behavior"
(i.e. anything could happen).
However, some C++ platfomrs may offer the possibility
to have automated runtime checks for such errors.
- In the above example, the executed code does not
actually need to access the *ptr object to execute
function geta() -- because no data member is accessed,
and geta() is *not* declared as virtual. So although
the behavior of the code is formally undefined, it
will just happen to run without error on must C++ platforms.
hth --Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <http://www.brainbench.com
Sep 8 '06 #2
Ivan Vecerina wrote:
So although
the behavior of the code is formally undefined, it
will just happen to run without error on must C++ platforms.

Yep, that's one of the insidious possibilities of undefined
behavior. It may work apparently without error... until some
day when something else changes (like you update to the next
version of the compiler) and BOOM! code suddenly breaks.
Sep 8 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Thomas Scheiderich | last post by:
I am trying to understand Session variables and ran into a question on how they work with data that is passed. I have an HTM file that calls an ASP file and sends the name either by GET or POST....
17
by: Medi Montaseri | last post by:
Hi, Given a collection of similar but not exact entities (or products) Toyota, Ford, Buick, etc; I am contemplating using the Abstraction pattern to provide a common interface to these products....
6
by: Martin | last post by:
I'd like to be able to get the name of an object instance from within a call to a method of that same object. Is this at all possible? The example below works by passing in the name of the object...
2
by: SouthSpawn | last post by:
I have the following situation. I am creating my own user control. This asp.net user control will have a method called "Read". When this method is being called from the page. It will read line...
4
by: Ray Dukes | last post by:
What I am looking to do is map the implementation of interface properties and functions to an inherited method of the base class. Please see below. ...
4
by: Alex Stevens | last post by:
Hi All, I'm writing a data access class in which I expose an SQlClient.SQLCommand's Parameters Collection via a property. How would I go about overloading the parameters.add method with one of...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
3
by: AWasilenko | last post by:
I'm still in the process of learning python via a handful of books I bought. One book I am reading just introduced Base Class Methods. I found that I needed more understanding on this concept and...
11
by: eBob.com | last post by:
I have this nasty problem with Shared methods and what I think of as "global storage" - i.e. storage declared outside of any subroutines or functions. In the simple example below this "global"...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
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
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...

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.