#include <iostream>
using namespace std;
class Person{
public:
virtual void print () { cout << "In parent" << endl; }
void onlyInPerson() { cout << "Only in Person/parent" <<
endl; }
};
class Student: public Person{
public:
Student() { cout << "Student constructor." << endl; }
void print() { cout << "In child" << endl; }
void onlyInChild() { cout << "Only in Child/Student" <<
endl; }
};
int main()
{
Person* p ;
Student* s;
s->onlyInChild( );
return 0;
}
Dec 14 '07
17 1489 am************* ********@gmail. com wrote:
Isin't the garbage this pointer dereferenced to to call the member
function onlyInChild() ?
Maybe, maybe not. Who cares?
There. Is. No. Defined. Behavior. For. Undefined. Behavior.
Say that over and over to yourself until you GET IT.
Brian
On Dec 14, 5:35 pm, "Default User" <defaultuse...@ yahoo.comwrote:
amit.bolakani.t echni...@gmail. com wrote:
Isin't the garbage this pointer dereferenced to to call the member
function onlyInChild() ?
Maybe, maybe not. Who cares?
There. Is. No. Defined. Behavior. For. Undefined. Behavior.
Say that over and over to yourself until you GET IT.
Brian
No worries Brian :) GOT IT!
Thanks for all your help guys. Appreciate it.
On 2007-12-14 23:33, am************* ********@gmail. com wrote:
On Dec 14, 5:05 pm, Vaclav Haisman <v.hais...@sh.c vut.czwrote:
>amit.bolakani. techni...@gmail .com wrote, On 14.12.2007 22:29:
#include <iostream>
using namespace std;
class Person{
public:
virtual void print () { cout << "In parent" << endl; }
void onlyInPerson() { cout << "Only in Person/parent" <<
endl; }
};
class Student: public Person{
public:
Student() { cout << "Student constructor." << endl; }
void print() { cout << "In child" << endl; }
void onlyInChild() { cout << "Only in Child/Student" <<
endl; }
};
int main()
{
Person* p ;
Student* s;
s->onlyInChild( );
return 0;
}
The programme compiles because it is well-formed and probably even runs because the methods actually do not touch anything in the object itself but your programme is invoking undefined behaviour.
-- VH
Ok, so I understand that its undefined behavior. However, is the
garbage this pointer dereferenced to call the member method?
Do you see 'this' (or any other members) mentioned anywhere in
onlyInChild()? If you do not, why would it be dereferenced?
--
Erik Wikström
On 14 Dec, 22:03, amit.bolakani.t echni...@gmail. com wrote:
On Dec 14, 4:59 pm, tragomaskhalos <dave.du.verg.. .@logicacmg.com >
wrote:
On 14 Dec, 21:45, amit.bolakani.t echni...@gmail. com wrote:
On Dec 14, 4:34 pm, David Côme <davidc...@wana doo.frwrote:
On Fri, 14 Dec 2007 22:29:32 +0100, <amit.bolakani. techni...@gmail .com>
wrote:
#include <iostream>
using namespace std;
class Person{
public:
virtual void print () { cout << "In parent" << endl; }
void onlyInPerson() { cout << "Only in Person/parent" <<
endl; }
};
class Student: public Person{
public:
Student() { cout << "Student constructor." << endl; }
void print() { cout << "In child" << endl; }
void onlyInChild() { cout << "Only in Child/Student" <<
endl; }
};
int main()
{
Person* p ;
Student* s;
s->onlyInChild( );
return 0;
}
I'm not sure but for me it's an undefined behaviour
It seems to compile and run fine! I'm pretty surprised and it throws
me off a bit in terms of how it runs fine?- Hide quoted text -
That it happens to work is no surprise - onlyInChild does not access
any members, and the method is not virtual, hence the garbage this
pointer is
not dereferenced. Neverthless it's still undefined behaviour.
Isin't the garbage this pointer dereferenced to to call the member
function onlyInChild() ?- Hide quoted text -
OK, my original reply wasn't detailed enough.
Others have said that this is UB but you are quite correctly
still wondering why it happend to work with your compiler.
The key is that the compiler is likely producing code something akin
to this:
//SOURCE:
// void Student::onlyIn Child() { cout << "Only in Child/Student" <<
endl; }
//COMPILER:
void __Student_onlyI nChild(__this) {
/* ... stuff that NEVER USES __this */
}
//SOURCE s->onlyInChild( ); // nb s is uninitialised ...
//COMPILER:
__Student_onlyI nChild(s); // nb s is NOT dereferenced
HTH
Hi am************* ********@gmail. com wrote:
Ok, so I understand that its undefined behavior. However, is the
garbage this pointer dereferenced to call the member method?
That's just one of the things that is undefined.
Markus
On 2007-12-15 04:36:30 -0500, tragomaskhalos
<da************ *@logicacmg.com said:
>
OK, my original reply wasn't detailed enough.
Others have said that this is UB but you are quite correctly
still wondering why it happend to work with your compiler.
The key is that the compiler is likely producing code something akin
to this:
Maybe. Speculating about why something whose behavior is undefined
happens to do what a naive programmer guessed it would do is generally
pointless. Undefined means undefined. Nothing more. The reason that it
"works" is pure accident. (That's the screed for beginning and
intermediate programmers.)
--
Pete
Roundhouse Consulting, Ltd. ( www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
( www.petebecker.com/tr1book)
On 15 Dec, 13:57, Pete Becker <p...@versatile coding.comwrote :
On 2007-12-15 04:36:30 -0500, tragomaskhalos
<dave.du.verg.. .@logicacmg.com said:
OK, my original reply wasn't detailed enough.
Others have said that this is UB but you are quite correctly
still wondering why it happend to work with your compiler.
The key is that the compiler is likely producing code something akin
to this:
Maybe. Speculating about why something whose behavior is undefined
happens to do what a naive programmer guessed it would do is generally
pointless. Undefined means undefined. Nothing more. The reason that it
"works" is pure accident. (That's the screed for beginning and
intermediate programmers.)
Well that's probably a good philosophy in the general case, but here
the OP accepted the undefinedness of the code but was clearly baffled
as to how it *could ever* have worked; seeing how a compiler might
realistically have treated the code so as to produce the observed
effect
is often valuable - when taken with the usual caveats. Otherwise one
is
left with a helpless feeling that it's all magic, which can only
dishearten the questioner and deter experimentation .
On 2007-12-15 18:14:15 -0500, tragomaskhalos
<da************ *@logicacmg.com said:
On 15 Dec, 13:57, Pete Becker <p...@versatile coding.comwrote :
>On 2007-12-15 04:36:30 -0500, tragomaskhalos <dave.du.verg. ..@logicacmg.co msaid:
>>OK, my original reply wasn't detailed enough. Others have said that this is UB but you are quite correctly still wondering why it happend to work with your compiler. The key is that the compiler is likely producing code something akin to this:
Maybe. Speculating about why something whose behavior is undefined happens to do what a naive programmer guessed it would do is generally pointless. Undefined means undefined. Nothing more. The reason that it "works" is pure accident. (That's the screed for beginning and intermediate programmers.)
Well that's probably a good philosophy in the general case, but here
the OP accepted the undefinedness of the code but was clearly baffled
as to how it *could ever* have worked; seeing how a compiler might
realistically have treated the code so as to produce the observed
effect
is often valuable - when taken with the usual caveats. Otherwise one
is
left with a helpless feeling that it's all magic, which can only
dishearten the questioner and deter experimentation .
Once you're in the realm of undefined behavior, it is all magic, unless
you're skills are advanced enough to analyze machine instructions.
That's not a useful exercise for beginners.
--
Pete
Roundhouse Consulting, Ltd. ( www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
( www.petebecker.com/tr1book) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Sabrina |
last post by:
Can someone help? I have been trying to get the hash_map
in C++ for .NET to work with strings and const char*. I
am using the const char* as the key and a pointer to
another class as the data type. I would like to be able
to assign unique names to each key and allow the user to
be able to search the hash_map to find that key and in so
doing, retrieve the information that is being pointed to
by the other class' pointer(data type). I can...
|
by: .pd. |
last post by:
If I have a control that adds itself to its container, does that break
encapsulation? Is it a bad thing?
Here's what I mean:
public class fred
{
public barny b;
public fred()
{
|
by: bic |
last post by:
When porting my Windows app to asp.net I get an System.Exception: Cannot
create ActiveX component. What function can I use instead? Thanks for your
comments.
--
bic
|
by: Frank Rizzo |
last post by:
I understand the basic premise: when the object is out of scope or has
been set to null (given that there are no funky finalizers), executing
GC.Collect will clean up your resources.
So I have a basic test. I read a bunch of data into a dataset by using
a command and a data adapter object, .Dispose(ing) as I go. The moment
the data is in the Dataset, the Mem Usage column in the Task Manager
goes up by 50 MB (which is about right). I...
|
by: michelle |
last post by:
I am experimenting with dynamically creating pages containing custom
controls in response to http requests caught by an http handler, in
place of writing and requesting aspx files. Specifically, I am doing
the following:
- mapping requests to an http handler
- instantiating a class hierarchy that, retrieves data, parses the URL
and performs some business processing
- instantiating the Page class and adding controls
- rendering the page...
| |
by: Gary Frank |
last post by:
What are the ramifications if I were to instantiate an object tens of
thousands of times and add them to an array? Or hundreds of thousands of
times? Do you know if the act of instantiating a class takes a lot of
storage or other resources? Would it be a severe performance penalty?
From the .Net help doc:
|
by: Barret Bonden |
last post by:
As an old programmer just now looking at VB.net I have a question:
How does one simply open one form from another ? I don't mean how does one
create a new instance of that form , but rather how to refer to THAT form ?
And having done this, how does one get data to and from that form ?
I have read the MS tutorial on this but it is senselessly complex; there
must be a simple, straightforward method, or I can only assume that MS has...
|
by: Nagesh |
last post by:
hi,
I have seen the winvnc(tightvnc server) source code in this I seen
that class member funtions are calling without instantiating the object
i.e. like vncService::ShowDefaultProperties() where vncService is a
class name not an refrence or instantiated object.
is the above notation is possible or not?if yes how should i declare
that class(vncService) so that i can call without instantiating the
object. If any of u know pls answer to...
|
by: Reckoner |
last post by:
would it be possible to use one of an object's methods without
initializing the object?
In other words, if I have:
class Test:
def __init__(self):
print 'init'
def foo(self):
print 'foo'
|
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: 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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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: 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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
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
| |