473,399 Members | 3,106 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,399 software developers and data experts.

Runtime polymorphism with references

Hello,

Can I have runtime polymorphism using references. I knew that runtime
polymorphism could be obtained only by pointers, but now I have tried
this in Visual C++ 8.0:

#include <iostream>

using namespace std;

class Shape
{
public:
virtual void draw()
{
cout<<"Draw Shape"<<endl;
};
Shape()
{
cout<<"new Shape"<<endl;
}
virtual
~Shape()
{
cout<<"delete Shape"<<endl;
}
};

class Circle: public Shape
{
public:
void draw()
{
cout<<"Draw Circle"<<endl;
}
Circle()
{
cout<<"new Circle"<<endl;
}
~Circle()
{
cout<<"delete Circle"<<endl;
}
};

int main()
{
Circle c;
Shape &s = c;
s.draw();

return 0;
}

What I get is:

new Shape
new Circle
Draw Circle
delete Circle
delete Shape

So, can I have runtime polymorphism with references?
The same output happens in g++. (version 3.3.1)

Thanks,
Doina Babu

Nov 22 '06 #1
7 4300
doina napsal:
Hello,

Can I have runtime polymorphism using references. I knew that runtime
polymorphism could be obtained only by pointers, but now I have tried
this in Visual C++ 8.0:

#include <iostream>

using namespace std;

class Shape
{
public:
virtual void draw()
{
cout<<"Draw Shape"<<endl;
};
Shape()
{
cout<<"new Shape"<<endl;
}
virtual
~Shape()
{
cout<<"delete Shape"<<endl;
}
};

class Circle: public Shape
{
public:
void draw()
{
cout<<"Draw Circle"<<endl;
}
Circle()
{
cout<<"new Circle"<<endl;
}
~Circle()
{
cout<<"delete Circle"<<endl;
}
};

int main()
{
Circle c;
Shape &s = c;
s.draw();

return 0;
}

What I get is:

new Shape
new Circle
Draw Circle
delete Circle
delete Shape

So, can I have runtime polymorphism with references?
The same output happens in g++. (version 3.3.1)

Thanks,
Doina Babu
Yes, it is correct in C++.

Nov 22 '06 #2
doina wrote:
Hello,

Can I have runtime polymorphism using references. I knew that runtime
polymorphism could be obtained only by pointers, but now I have tried
this in Visual C++ 8.0:

#include <iostream>

using namespace std;

class Shape
{
public:
virtual void draw()
{
cout<<"Draw Shape"<<endl;
};
Shape()
{
cout<<"new Shape"<<endl;
}
virtual
~Shape()
{
cout<<"delete Shape"<<endl;
}
};

class Circle: public Shape
{
public:
void draw()
{
cout<<"Draw Circle"<<endl;
}
Circle()
{
cout<<"new Circle"<<endl;
}
~Circle()
{
cout<<"delete Circle"<<endl;
}
};

int main()
{
Circle c;
Shape &s = c;
s.draw();

return 0;
}

What I get is:

new Shape
new Circle
Draw Circle
delete Circle
delete Shape

So, can I have runtime polymorphism with references?
The same output happens in g++. (version 3.3.1)

Thanks,
Doina Babu
new to c++?
that certainly works .
have fun.
why do u call it runtime polymorphism?
heared anything about compile time polymorphism???

Nov 22 '06 #3
terminator wrote:
new to c++?
no
that certainly works .
Yes, I know it works. It's what I said in my first post. :) I was
looking for an explanation (not just for valuable words... )
why do u call it runtime polymorphism?
I called it runtime polymorphism because the decision as to which
version of a method will be executed is based on the actual type of
object whose reference is stored in the reference variable, and not on
the type of the reference variable on which the method is invoked.
That's runtime polymorphism.
Until now, I was using pointers to have runtime polymorphism, like
this:
Shape *s = new Circle();
s->draw();
delete s;

I guess since references are also pointers internally, this means it
should work for references too.
heared anything about compile time polymorphism???
Why is it compile-time polymorphism?
How does it know the compiler that s is actually a Circle object and
not a Shape ... at compile time... ?
Indeed, I can use compile-time polymorphism and increase performance
but in another implementation ... one using templates...

Regards,
Doina Babu

Nov 22 '06 #4

doina wrote:
terminator wrote:
new to c++?
no
that certainly works .
Yes, I know it works. It's what I said in my first post. :) I was
looking for an explanation (not just for valuable words... )
why do u call it runtime polymorphism?
I called it runtime polymorphism because the decision as to which
version of a method will be executed is based on the actual type of
object whose reference is stored in the reference variable, and not on
the type of the reference variable on which the method is invoked.
That's runtime polymorphism.
Until now, I was using pointers to have runtime polymorphism, like
this:
Shape *s = new Circle();
s->draw();
delete s;

I guess since references are also pointers internally, this means it
should work for references too.
heared anything about compile time polymorphism???
Why is it compile-time polymorphism?
How does it know the compiler that s is actually a Circle object and
not a Shape ... at compile time... ?
Indeed, I can use compile-time polymorphism and increase performance
but in another implementation ... one using templates...

Regards,
Doina Babu
A persian proverb says that its wrong to ask if u already know the
answer.
u r partially right but references r not always treated as internal
pointers
auto-optimiation might remove unneccesary code.

Nov 22 '06 #5
terminator <fa***********@gmail.comwrote:
heared anything about compile time polymorphism???
Templates are sometimes referred to as "compile-time polymorphism".

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Nov 22 '06 #6

Marcus Kwok wrote:
terminator <fa***********@gmail.comwrote:
heared anything about compile time polymorphism???

Templates are sometimes referred to as "compile-time polymorphism".

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Hmmm.I should have guessed.

thx 4 remark.

Nov 23 '06 #7
On Nov 22, 4:25 pm, "doina" <doina.b...@gmail.comwrote:
terminator wrote:
new to c++?no
that certainly works .Yes, I know it works. It's what I said in my first post. :) I was
looking for an explanation (not just for valuable words... )
Explanation of what? Why it works? Because C++ is designed to work that
way, in fact when using exceptions you should always catch by reference
because of polymorfism. Whoever told you that pointers is the only way
was wrong, and now you know better than him.

--
Erik Wikström

Nov 23 '06 #8

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

Similar topics

18
by: Ken | last post by:
Hi. Can anyone refer me to any articles about the compatibility between c++ polymorphism and real-time programming? I'm currently on a real-time c++ project, and we're having a discussion...
4
by: The Directive | last post by:
I can't understand why in this folling example the message "Base.Draw()" is printed. Shouldn't "Derive.Draw" be printed because of polymorphism? How can I rewrite this example using a vector to...
4
by: Leslaw Bieniasz | last post by:
Cracow, 20.10.2004 Hello, As far as I understand, the generic programming basically consists in using templates for achieving a static polymorphism of the various code fragments, and their...
21
by: ravinderthakur | last post by:
hi experts, just out of curosity why polymorphism is allowed to work only with pointers and references to the objects and not with the objects itself. what are the reasons behind...
53
by: Alf P. Steinbach | last post by:
So, I got the itch to write something more... I apologize for not doing more on the attempted "Correct C++ Tutorial" earlier, but there were reasons. This is an UNFINISHED and RAW document,...
4
by: LP | last post by:
Hi, I understand the concept/definition of polymorphism. But what does the term "runtime polymorphism" mean? I was asked to define it during a technical interview. I gave a guy vanilla definition...
4
by: arunvnk | last post by:
It is not quite clear why the compiler uses rntime polymorphism when the virtual keyword is specified. Since the virtual Keyword is specified,why can't the compiler decide at the runtime...
16
by: desktop | last post by:
I have read that using templates makes types know at compile time and using inheritance the types are first decided at runtime. The use of pointers and casts also indicates that the types will...
1
weaknessforcats
by: weaknessforcats | last post by:
Introduction Polymorphism is the official term for Object-Oriented Programming (OOP). Polymorphism is implemented in C++ by virtual functions. This article uses a simple example hierarchy which...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
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...

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.