473,888 Members | 1,499 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1626
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...@com Acast.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...@com Acast.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.comwro te:
On May 29, 2:14*am, Victor Bazarov <v.Abaza...@com Acast.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.comwro te:
On May 29, 2:14 am, Victor Bazarov <v.Abaza...@com Acast.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 objektorientier ter Datenverarbeitu ng
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.comwro te:


On May 29, 2:14*am, Victor Bazarov <v.Abaza...@com Acast.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.no wrote:
* James Kanze:
On May 29, 2:15 am, "A. Bolmarcich" <agge...@earl-grey.cloud9.net >
wrote:
On 2008-05-28, Arindam <arindam.muker. ..@gmail.comwro te:
On May 29, 2:14 am, Victor Bazarov <v.Abaza...@com Acast.netwrote:
Arindam wrote:
#include <cstdio>
>>>struct Test {
void bar() {
foo();
}
private:
virtual void foo() {
printf("Test \n");
}
};
>>>struct Test2 : public Test {
void foo() {
printf("Test 2\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 objektorientier ter Datenverarbeitu ng
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
1702
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 the data structure like the following to represent automatons, map<Set, map<Input, Set> > However, for deterministic finite automaton, I derive the class "State" from
4
27694
by: Maurice Termeer | last post by:
Hi, suppose i've got this: class a { public: int n; }; class b : public a { public: };
20
2243
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, cast to void *. Specifically, template <typename T> void f(T * t) { void * p = dynamic_cast<void *>(t) ; } will not compile if T isn't of a class that has somewhere at least
1
2267
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 out that detecting whether T is polymorphic clashes with a new requirement, that T be allowed to not be complete. Last week, this code used to compile:
7
3920
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 method which returns a new object of the derived type. The problem I've got is that I now need to polymorphically clone a derived class object, but I don't want to write a separate 'clone' method for each of these 150 classes. Instead, I thought I...
7
2087
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 people consider Access Forms to be Polymorphic? James A. Fortune
12
1457
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 property 'X'. Can't override Base class 'X' because of different structure. So you Shadow the base class 'X' in the derived class. Pass an instance of the derived class to a function.
6
1523
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 have a virtual destructor from a previous post by me, and thus it is designed as a safe way. As the PointVector is non polymorphic (and its runtime typeid also shows the same), can I assume it is as efficient as a typedef to vector<Point? i.e no...
11
1489
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. Then in my base class I for example have a pure virtual function called eg PerformAction. Then in each message class I implement PerformAction. The base class works out what the type of message is and then calls the correct inherited class...
0
9961
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11180
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10778
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...
1
10885
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7990
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
5817
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
6014
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4244
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3252
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.