Runtime polymorphism with references

D

doina

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
 
O

Ondra Holub

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++.
 
T

terminator

doina said:
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???
 
D

doina

terminator said:
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
 
T

terminator

doina said:
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... )

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.

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.
 
E

eriwik

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top