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

Override a virtual member in a instance?


Hi!
A derived class can override a method in the base class it inherits for,
and even my dog knows that. More incredibly, I know and understand it
too.

But, can a (of course virtual in this case) method be overriden in an
instance instead? For example, to provide some callbacks to a class.

Should I use virtual functions instead? How do I pass the this pointer
when casting it from or to in any useful way seems to break every C++
language rule? As a normal parameter? But then it doesn't really look
so OOP anymore.

Thanks a lot,
Tony

Aug 24 '06 #1
2 1632
to**@donotspamme.ar wrote:
A derived class can override a method in the base class it inherits
for, and even my dog knows that. More incredibly, I know and
understand it too.

But, can a (of course virtual in this case) method be overriden in an
instance instead? For example, to provide some callbacks to a class.
No. For that you usually store pointers to [member] functions and assign
them different values based on the need, per instance.
Should I use virtual functions instead?
Instead of what? Instead of virtual functions? Uh... Yes!.. I mean,
no... I mean... Could you rephrase, please?
How do I pass the this pointer
when casting it from or to in any useful way seems to break every C++
language rule? As a normal parameter? But then it doesn't really look
so OOP anymore.
Huh? I think I don't understand (where does it place me relative to
your dog?), could you give an example in C++?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 24 '06 #2
to**@donotspamme.ar wrote:
>
Hi!
A derived class can override a method in the base class it inherits for,
and even my dog knows that. More incredibly, I know and understand it
too.

But, can a (of course virtual in this case) method be overriden in an
instance instead? For example, to provide some callbacks to a class.

Should I use virtual functions instead? How do I pass the this pointer
when casting it from or to in any useful way seems to break every C++
language rule? As a normal parameter? But then it doesn't really look
so OOP anymore.
Virtual functions are type bound (although to the dynamic type). You can use
a function pointer or a member variable whose type is a function object if
you want to override the semantics of the call per instance.

Best

Kai-Uwe Bux
Aug 24 '06 #3

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

Similar topics

3
by: Daniel Graifer | last post by:
Why doesn't c++ support virtual data? It supports class data of type pointer to function (that's what virtual functions really are). In terms of implementation, why can't I have other types of...
22
by: Ruben Van Havermaet | last post by:
Hi, I have a problem using member functions in derived classes that override virtual member functions of base classes. The following pieces of (simplified) code don't work. Can anybody give...
8
by: Gawelek | last post by:
When I write this code namespace WindowsApplication1 { public class Form1 : System.Windows.Forms.Form { (...) /// <summary> /// Clean up any resources being used. /// </summary>
5
by: Eric Johannsen | last post by:
I have a simple object that inherits from CollectionBase and overrides the Count property: namespace MyTest { public class CollTest : System.Collections.CollectionBase { public override int...
8
by: JPRoot | last post by:
Hi M. Jeffrey Tan, Just hopping you didn't forget me? :) Thanks JPRoot ----- \"Jeffrey Tan\" wrote: -----
5
by: Darius | last post by:
I'm writing here in hopes that someone can explain the difference between the new and virtual/override keywords in C#. Specifically, what is the difference between this: public class Window {...
5
by: Marcel Hug | last post by:
Hi NG ! I'm new in C# and I'm reading a book about the fundamentals and concepts. In the chapter Methods it's written to use virtual, if i would like to override the method in a subclass. This...
14
by: Dave Booker | last post by:
It looks like the language is trying to prevent me from doing this sort of thing. Nevertheless, the following compiles, and I'd like to know why it doesn't work the way it should: public class...
6
by: Ole Nielsby | last post by:
Given a class hierarchy: class A{public: virtual void m();}; class B : public A {public: virtual void m();}; class C : public B {public: virtual void m();}; //overrides m() class D : public B...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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: 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
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,...
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...

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.