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

How does this work without instantiating an object of the Studentclass?

#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 #1
17 1465
On Dec 14, 4:29 pm, 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;

}- Hide quoted text -

- Show quoted text -
Although function is getting called, But its undefined behavior.
Dec 14 '07 #2
On Fri, 14 Dec 2007 22:29:32 +0100, <am*********************@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
Dec 14 '07 #3
On Dec 14, 4:34 pm, David Côme <davidc...@wanadoo.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?
Dec 14 '07 #4
On Dec 14, 4:45 pm, amit.bolakani.techni...@gmail.com wrote:
On Dec 14, 4:34 pm, David Côme <davidc...@wanadoo.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?
Did any of you guys try this with your compiler. I tried it with g++
3.4.4 on cygwin.
Dec 14 '07 #5
am*********************@gmail.com wrote:

int main()
{
Person* p ;
Student* s;
s->onlyInChild();
return 0;
}
It's Undefined Behavior to deference an invalid pointer. There is no
defined behavior for Undefined Behavior. It is not a syntax error. It
is not a constraint violation. There is no required diagnostic.


Brian
Dec 14 '07 #6
On 14 Dec, 21:45, amit.bolakani.techni...@gmail.com wrote:
On Dec 14, 4:34 pm, David Côme <davidc...@wanadoo.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.
Dec 14 '07 #7
On Dec 14, 4:59 pm, tragomaskhalos <dave.du.verg...@logicacmg.com>
wrote:
On 14 Dec, 21:45, amit.bolakani.techni...@gmail.com wrote:
On Dec 14, 4:34 pm, David Côme <davidc...@wanadoo.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() ?
Dec 14 '07 #8
am*********************@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
Dec 14 '07 #9
On Dec 14, 5:05 pm, Vaclav Haisman <v.hais...@sh.cvut.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?
Dec 14 '07 #10
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
Dec 14 '07 #11
On Dec 14, 5:35 pm, "Default User" <defaultuse...@yahoo.comwrote:
amit.bolakani.techni...@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.
Dec 14 '07 #12
On 2007-12-14 23:33, am*********************@gmail.com wrote:
On Dec 14, 5:05 pm, Vaclav Haisman <v.hais...@sh.cvut.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
Dec 15 '07 #13
On 14 Dec, 22:03, amit.bolakani.techni...@gmail.com wrote:
On Dec 14, 4:59 pm, tragomaskhalos <dave.du.verg...@logicacmg.com>
wrote:


On 14 Dec, 21:45, amit.bolakani.techni...@gmail.com wrote:
On Dec 14, 4:34 pm, David Côme <davidc...@wanadoo.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::onlyInChild() { cout << "Only in Child/Student" <<
endl; }
//COMPILER:
void __Student_onlyInChild(__this) {
/* ... stuff that NEVER USES __this */
}

//SOURCE s->onlyInChild(); // nb s is uninitialised ...
//COMPILER:
__Student_onlyInChild(s); // nb s is NOT dereferenced

HTH

Dec 15 '07 #14
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
Dec 15 '07 #15
On 2007-12-15 04:36:30 -0500, tragomaskhalos
<da*************@logicacmg.comsaid:
>
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)

Dec 15 '07 #16
On 15 Dec, 13:57, Pete Becker <p...@versatilecoding.comwrote:
On 2007-12-15 04:36:30 -0500, tragomaskhalos
<dave.du.verg...@logicacmg.comsaid:
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.

Dec 15 '07 #17
On 2007-12-15 18:14:15 -0500, tragomaskhalos
<da*************@logicacmg.comsaid:
On 15 Dec, 13:57, Pete Becker <p...@versatilecoding.comwrote:
>On 2007-12-15 04:36:30 -0500, tragomaskhalos
<dave.du.verg...@logicacmg.comsaid:
>>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)

Dec 16 '07 #18

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

Similar topics

5
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...
2
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() {
4
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
9
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...
0
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...
6
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...
17
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...
3
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...
3
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'
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
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: 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
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
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.