473,769 Members | 4,297 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1486
On Dec 14, 4:29 pm, amit.bolakani.t echni...@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...@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?
Dec 14 '07 #4
On Dec 14, 4:45 pm, 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?
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.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.
Dec 14 '07 #7
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() ?
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.c vut.czwrote:
amit.bolakani.t echni...@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

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

Similar topics

5
4585
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...
2
2319
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
6397
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
8557
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...
0
1651
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...
6
1627
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:
17
8628
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...
3
4945
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...
3
1844
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
9422
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,...
0
10036
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 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...
0
9855
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 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...
0
8863
agi2029
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...
1
7404
isladogs
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...
0
6662
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();...
0
5294
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...
0
5444
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3952
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 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.