473,651 Members | 2,566 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Virtual Funtion Questions

Case 1:
==============

class MyClass
{
public:
virtual void MyVirt();
};

class MyChildClass : public MyClass
{
public:
void MyVirt();
};

If MyClass contains a virtual function which will be overridden by it
children classes:

MyChildClass *MChildC = new MyChildClass();
MyClass* ptr = MChildC;
ptr->MyVirt();//MyChildClass::M yVirt() called

Otherwise, the overriding function, MyVirt(), in MyChildClass will not be
executed?

Everything fine. Please read on...
Case 2:
=============

The same two classes as in Case 1.

MyChildClass MCC;
MyClass* ptr = &MCC;
ptr->MyVirt();

Will the staement, ptr->MyVirt(), actually call MyChildClass::M yVirt()?
Is this a general practice of polymorphism in C++ like in Case 1?

Now, this leads me to another question:

Why such a "Pointer"/"Reference" requirement needed to cause a late binding?
Case 3:
==============

Now we make some changes for the above two classes:

class MyClass
{
public:
void MyNonVirt() { MyVirt(); }

private:
virtual void MyVirt();
};

class MyChildClass : public MyClass
{
private:
void MyVirt();
};

Note that MyNonVirt() calls MyVirt() and MyVirt() is private member now.

First, is this a good design at all?

How should I do to make sue that MyChildClass::M yVirt() is called at
runtime?
Thanks!

Jul 22 '05 #1
3 1579

"garyolsen" <ga*******@yaho o.com> wrote in message news:Ft******** **************@ bgtnsc05-news.ops.worldn et.att.net...

Why such a "Pointer"/"Reference" requirement needed to cause a late binding?
The only way you can get to a an object whose dynamic type is different from
the static type is to use (at least implcitly) a pointer or reference.

MCC.MyVirt(); // no brainer, MCC is of type MyChildClass
ptr->MyVirt(); // must figure out dymaic type of *ptr at runtime
Note that MyNonVirt() calls MyVirt() and MyVirt() is private member now.
Protection doesn't have any bearing on virtual. You can't call MyVirt() from
outside MyClass if it's private (makes no different if virtual or not)>
How should I do to make sue that MyChildClass::M yVirt() is called at


It will be called if you call MyNonVirt() in the context of a MyChildClass object.
The way function calling works is like this:

1. First for the static type of the call, the name is looked up. If you call through
a pointer of type MyClass, then it finds MyCLass::MyNonF irst.
2. Possible overloads are checked (you only have the one).
3. Access is checked (OK, public).
4. If virtual, the final overrider is substituted (it's not).

Ok, now you're executing MyNonVirt. You go through the process again:

1. Lookup Name: Find MyClass::MyVirt
2. Check overloads (only one).
3. Check access (fine, private but being called from within class)
4. If virtual, substitute final overrider (yes, it's virtual, object is of type MyChildClass so My ChildClass::MyV irt is run).
Jul 22 '05 #2
"garyolsen" <ga*******@yaho o.com> wrote...
Case 1:
==============

class MyClass
{
public:
virtual void MyVirt();
};

class MyChildClass : public MyClass
{
public:
void MyVirt();
};

If MyClass contains a virtual function which will be overridden by it
children classes:

MyChildClass *MChildC = new MyChildClass();
MyClass* ptr = MChildC;
ptr->MyVirt();//MyChildClass::M yVirt() called

Otherwise, the overriding function, MyVirt(), in MyChildClass will not be
executed?
Yes. If the function is not virtual, there is no "overriding " of it,
the MyChildClass' function with the same name would _hide_ the MyClass'
one.

Everything fine. Please read on...
Case 2:
=============

The same two classes as in Case 1.

MyChildClass MCC;
MyClass* ptr = &MCC;
ptr->MyVirt();

Will the staement, ptr->MyVirt(), actually call MyChildClass::M yVirt()?
Of course. What's the difference with the above, really? That
the object was created in freestore instead of automatically? It
makes no difference.
Is this a general practice of polymorphism in C++ like in Case 1?
Similar.
Now, this leads me to another question:

Why such a "Pointer"/"Reference" requirement needed to cause a late binding?

Because without it (by value) the _slicing_ occurs, and there is no
original object left.
Case 3:
==============

Now we make some changes for the above two classes:

class MyClass
{
public:
void MyNonVirt() { MyVirt(); }

private:
virtual void MyVirt();
};

class MyChildClass : public MyClass
{
private:
void MyVirt();
};

Note that MyNonVirt() calls MyVirt() and MyVirt() is private member now.

First, is this a good design at all?
It's _a_ design. There has to be a purpose to it. If such design
exists, there must have been a purpose. Don't ask us to guess it.
How should I do to make sue that MyChildClass::M yVirt() is called at
runtime?


The same way. If your pointer to MyClass is a pointer to a _subobject_
of an object of MyChildClass, then calling MyNonVirt for it will cause
the internal call to MyVirt to be dispatched _dynamically_. So, no
special action necessary.

Victor
Jul 22 '05 #3

"garyolsen" <ga*******@yaho o.com> wrote in message
news:Ft******** **************@ bgtnsc05-news.ops.worldn et.att.net...

Case 2:
=============

The same two classes as in Case 1.

MyChildClass MCC;
MyClass* ptr = &MCC;
ptr->MyVirt();

Will the staement, ptr->MyVirt(), actually call MyChildClass::M yVirt()?
Is this a general practice of polymorphism in C++ like in Case 1?
Yes, it might not be done exactly that way - it might be a parameter on a
function call, but the idea is the same.
Why such a "Pointer"/"Reference" requirement needed to cause a late binding?

Because there's isn't an alternative. How else would you cause late
binding?
Case 3:
==============

Now we make some changes for the above two classes:

class MyClass
{
public:
void MyNonVirt() { MyVirt(); }

private:
virtual void MyVirt();
};

class MyChildClass : public MyClass
{
private:
void MyVirt();
};

Note that MyNonVirt() calls MyVirt() and MyVirt() is private member now.

First, is this a good design at all?
It looks OK to me, depending on what you need to accomplish.
How should I do to make sue that MyChildClass::M yVirt() is called at
runtime?


As you have it written, it will be - assuming an object of type MyChildClass
such as
MyClass* pClass = new MyChildClass;
pClass->MyNonVirt();
Jul 22 '05 #4

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

Similar topics

3
11434
by: scott | last post by:
hi all, hope some one can help me. Ill try and explain what im trying to do as best i can. i have a parent class that has a vertual function, lets call it virtual int A(). That vertual function does somthing that must be done. This meens that when a child class inherits the class and creates its own vertual int A() the parent class must also be called. the prob is i can not use the base class name and then its functino name after it...
9
1903
by: jlopes | last post by:
There seems to bet no diff between a vitual method and an inheirited method. class A_Base { public: virtual void filter(){ /* some code */ } }; class D_of_A_Base : public A_Base {
9
13041
by: kish_nand | last post by:
Could someone please explain me the concept behind virtual functions and vtables. I am little confused about this. Please refer to the following code and tell me how many virtual tables would be created and what they would contain: class base { virtual void display() { cout<<"base display"<<endl;
10
1335
by: Serg | last post by:
Every time i am trying to step into virtual funtion debugger tells me "there is no source code available" and disassemble window appears. If function is non virtual debugger succefully steps into it and shows source code. Is there any compiler key that can fix it? Please help. Thanks in adv.
20
2402
by: Nemanja Trifunovic | last post by:
Something I don't get it: Say we have: ref class Base { virtual void SomeVirtualFunction() {Console::WriteLine(L"Base");} public: void SomeAccessibleFunction() {SomeVirtualFunction();}
8
2888
by: rakoo | last post by:
I want to question about this virtual keyword , what is neccessty of it .. when base class ponter or simply object assingned to derived class object ,we never want that base class funtion by base class object or pointer which pointing to derived class object Are we ? then why stroupstrop has given this keyword
1
286
by: Rahul K | last post by:
Hi all I tried running the following code: #include<iostream.h> class Base { public: virtual void func()
3
11832
by: Jimmy | last post by:
I was browsing the C++ FAQ Lite the other day when I came accross question #23.4 (http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.4). The question was "When should someone use private virtuals?", and the author answered with "almost never, but there are reasons". So, I am asking, what are the reasons. It seems illogical to have one; the point of a virtual function is to be overriden in a derived class, and the...
1
1837
by: sridhard2406 | last post by:
Hi All, I am new to c++, please find my program below and my doubt as mentioned below. #include <iostream> using namespace std; class base { public:
0
8352
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
8802
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...
1
8465
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,...
0
8579
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
7297
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...
0
4144
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
4283
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2699
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
2
1587
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.