473,403 Members | 2,183 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,403 software developers and data experts.

Isn't this a polymorphic call?

#include <cstdio>

struct Test {
void bar() {
foo();
}
private:
virtual void foo() {
printf("Test\n");
}
};

struct Test2 : public Test {
void foo() {
printf("Test2\n");
}
};

int main(int argc, char* argv[])
{
Test2 v;
v.bar();
}

Does the call to v.bar() amount to a polymorphic invocation of foo()?
If not, I am guessing the following will be polymorphic:

int main(int argc, char* argv[])
{
Test2 v;
Test * pv = &v;
pv->bar();
}

bar takes a _this_ pointer whose static type is Test*. Therefore I am
reasoning that v.bar is something like:

bar(Test* this) being invoked as:

Test2 v;
bar(&v);

But inside bar, the static type of _this_ is really Test*. But
invoking foo() on it will resolve to Test2::foo. So both should be
polymorphic. Ami I correct?

Cheers,
Arindam
Jun 27 '08 #1
7 1604
Arindam wrote:
#include <cstdio>

struct Test {
void bar() {
foo();
}
private:
virtual void foo() {
printf("Test\n");
}
};

struct Test2 : public Test {
void foo() {
printf("Test2\n");
}
};

int main(int argc, char* argv[])
{
Test2 v;
v.bar();
}

Does the call to v.bar() amount to a polymorphic invocation of foo()?
Have you tried it? If you have, what happened? If not, why not?
If not, I am guessing the following will be polymorphic:

int main(int argc, char* argv[])
{
Test2 v;
Test * pv = &v;
pv->bar();
}

bar takes a _this_ pointer whose static type is Test*. Therefore I am
reasoning that v.bar is something like:

bar(Test* this) being invoked as:

Test2 v;
bar(&v);

But inside bar, the static type of _this_ is really Test*. But
invoking foo() on it will resolve to Test2::foo. So both should be
polymorphic. Ami I correct?
I think so.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #2
On May 29, 2:14*am, Victor Bazarov <v.Abaza...@comAcast.netwrote:
Arindam wrote:
#include <cstdio>
struct Test {
void bar() {
foo();
}
private:
virtual void foo() {
printf("Test\n");
}
};
struct Test2 : public Test {
void foo() {
printf("Test2\n");
}
};
int main(int argc, char* argv[])
{
Test2 v;
v.bar();
}
Does the call to v.bar() amount to a polymorphic invocation of foo()?

Have you tried it? *If you have, what happened? *If not, why not?

Yes - it prints Test2. So this should be polymorphic right?
>

If not, I am guessing the following will be polymorphic:
int main(int argc, char* argv[])
{
Test2 v;
Test * pv = &v;
pv->bar();
}
bar takes a _this_ pointer whose static type is Test*. Therefore I am
reasoning that v.bar is something like:
bar(Test* this) being invoked as:
Test2 v;
bar(&v);
But inside bar, the static type of _this_ is really Test*. But
invoking foo() on it will resolve to Test2::foo. So both should be
polymorphic. Ami I correct?

I think so.
The same result here.
Jun 27 '08 #3
Arindam wrote:
On May 29, 2:14 am, Victor Bazarov <v.Abaza...@comAcast.netwrote:
>Arindam wrote:
>>#include <cstdio>
struct Test {
void bar() {
foo();
}
private:
virtual void foo() {
printf("Test\n");
}
};
struct Test2 : public Test {
void foo() {
printf("Test2\n");
}
};
int main(int argc, char* argv[])
{
Test2 v;
v.bar();
}
Does the call to v.bar() amount to a polymorphic invocation of foo()?
Have you tried it? If you have, what happened? If not, why not?


Yes - it prints Test2. So this should be polymorphic right?
Of course. Try removing 'virtual'.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #4
On 2008-05-28, Arindam <ar**************@gmail.comwrote:
On May 29, 2:14*am, Victor Bazarov <v.Abaza...@comAcast.netwrote:
>Arindam wrote:
#include <cstdio>
struct Test {
void bar() {
foo();
}
private:
virtual void foo() {
printf("Test\n");
}
};
struct Test2 : public Test {
void foo() {
printf("Test2\n");
}
};
int main(int argc, char* argv[])
{
Test2 v;
v.bar();
}
Does the call to v.bar() amount to a polymorphic invocation of foo()?

Have you tried it? *If you have, what happened? *If not, why not?


Yes - it prints Test2. So this should be polymorphic right?
It is not polymorphic according to the inventor of C++, who wrote in
"The C++ Programming Language (Third Edition)":

"To get polymorphic behavior in C++, the member functions called must
be virtual and objects must be manipulated through pointers or
references. When manipulating an object directly (rather than through
a pointer or reference), its exact type is known by the compiler so
that run-time polymorphism is not needed."
Jun 27 '08 #5
On May 29, 2:15 am, "A. Bolmarcich" <agge...@earl-grey.cloud9.net>
wrote:
On 2008-05-28, Arindam <arindam.muker...@gmail.comwrote:
On May 29, 2:14 am, Victor Bazarov <v.Abaza...@comAcast.netwrote:
Arindam wrote:
#include <cstdio>
struct Test {
void bar() {
foo();
}
private:
virtual void foo() {
printf("Test\n");
}
};
struct Test2 : public Test {
void foo() {
printf("Test2\n");
}
};
int main(int argc, char* argv[])
{
Test2 v;
v.bar();
}
Does the call to v.bar() amount to a polymorphic
invocation of foo()?
Have you tried it? If you have, what happened? If not, why not?
Yes - it prints Test2. So this should be polymorphic right?
It is not polymorphic according to the inventor of C++, who
wrote in "The C++ Programming Language (Third Edition)":
"To get polymorphic behavior in C++, the member functions called must
be virtual and objects must be manipulated through pointers or
references. When manipulating an object directly (rather than through
a pointer or reference), its exact type is known by the compiler so
that run-time polymorphism is not needed."
And? He's calling foo() through a pointer (this), and the call
is resolved polymorphicly.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #6
On May 29, 5:15*am, "A. Bolmarcich" <agge...@earl-grey.cloud9.net>
wrote:
On 2008-05-28, Arindam <arindam.muker...@gmail.comwrote:


On May 29, 2:14*am, Victor Bazarov <v.Abaza...@comAcast.netwrote:
Arindam wrote:
#include <cstdio>
struct Test {
void bar() {
foo();
}
private:
virtual void foo() {
printf("Test\n");
}
};
struct Test2 : public Test {
void foo() {
printf("Test2\n");
}
};
int main(int argc, char* argv[])
{
Test2 v;
v.bar();
}


It is not polymorphic according to the inventor of C++, who wrote in
"The C++ Programming Language (Third Edition)":

"To get polymorphic behavior in C++, the member functions called must
be virtual and objects must be manipulated through pointers or
references. *When manipulating an object directly (rather than through
a pointer or reference), its exact type is known by the compiler so
that run-time polymorphism is not needed."- Hide quoted text -
As Victor Bazarov suggested, removing the "virtual" keyword is a crisp
test. If you remove it, you will see "Test" being printed instead of
Test2.
Jun 27 '08 #7
On May 29, 10:30 am, "Alf P. Steinbach" <al...@start.nowrote:
* James Kanze:
On May 29, 2:15 am, "A. Bolmarcich" <agge...@earl-grey.cloud9.net>
wrote:
On 2008-05-28, Arindam <arindam.muker...@gmail.comwrote:
On May 29, 2:14 am, Victor Bazarov <v.Abaza...@comAcast.netwrote:
Arindam wrote:
#include <cstdio>
>>>struct Test {
void bar() {
foo();
}
private:
virtual void foo() {
printf("Test\n");
}
};
>>>struct Test2 : public Test {
void foo() {
printf("Test2\n");
}
};
>>>int main(int argc, char* argv[])
{
Test2 v;
v.bar();
}
>>>Does the call to v.bar() amount to a polymorphic
invocation of foo()?
>>Have you tried it? If you have, what happened? If not, why not?
>Yes - it prints Test2. So this should be polymorphic right?
It is not polymorphic according to the inventor of C++, who
wrote in "The C++ Programming Language (Third Edition)":
"To get polymorphic behavior in C++, the member functions
called must be virtual and objects must be manipulated
through pointers or references. When manipulating an
object directly (rather than through a pointer or
reference), its exact type is known by the compiler so that
run-time polymorphism is not needed."
And? He's calling foo() through a pointer (this), and the
call is resolved polymorphicly.
I think the question is (nearly) meaningless. The standard
doesn't guarantee how any call is resolved at the machine code
level. E.g. in this case the compiler may inline everything
so that there's no call instruction.
I almost answered like that myself. The real question is: what
does he mean by polymorphic behavior. In the end, whatever code
the compiler generates, however, the call must resolve to the
dynamic type, and not the static type, because the function is
virtual. How the compiler ensures this is its business.

And that, IMHO, is the real definition of polymorphism; the
behavior corresponds to that of the dynamic type of the object.
(Of course, we both agree that if the compiler can determine
what the dynamic type will be, then it can generate different,
and typically more efficient, code than if it can't.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #8

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

Similar topics

1
by: Daniel Suen | last post by:
Hi All, I am trying to implement various kinds of finite automaton simulators; I have abstract the transition mappings to be from a "Set" to another "Set" taking on some "Input". I come up with...
4
by: Maurice Termeer | last post by:
Hi, suppose i've got this: class a { public: int n; }; class b : public a { public: };
20
by: verec | last post by:
One problem I've come accross in designing a specific version of auto_ptr is that I have to disntiguish between "polymorphic" arguments and "plain" ones, because the template has to, internally,...
1
by: verec | last post by:
Last week I asked here how I could detect that a T was polymorphic, and received very thoughtful and useful replies that I used straight away. Thanks to all who answered. This week, it turns...
7
by: Mr. Ed | last post by:
I have a base class which has about 150 derived classes. Most of the derived classes are very similar, and many don't change the base class at all. All the derived classes have a unique factory...
7
by: James Fortune | last post by:
In response to different users or situations (data context) I transform the appearance and characteristics of Access Forms through code. This seems to fit in with the idea of polymorphism. Do...
12
by: Bob | last post by:
Hi, 'Shadowed' properties are not polymorphic. (See thread 'Inheritance and late binding') They should be. Problem: Base class has read only property 'X'. Derived class must have read / write...
6
by: toton | last post by:
Hi, I am inheriting a vector<Pointclass as a PointVector non-polymorphically, to add several additional functionalities to it. I know the dangers of inheriting a container class, as it doesn't...
11
by: Angus | last post by:
I am developing a server which receives a range of different messages. There are about 12 different message types so I thought that a good idea would be to devise a class for each message type....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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
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,...
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.