473,387 Members | 1,621 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.

special kind of virtual method


I often run into situation where I would like to have a method such as that
has derived behavior similar to destructors in c++, is that possible?

public BaseClass
{
private int m_dataInBase;
public BaseClass()
{m_dataInBase = 0}
public void SetDataBase( int val ) { m_data = val; }
virtual public void ClearData()
{
m_dataInBase = 0;
}
}

public DerivedClass
{
private int m_dataInDerived;
public DerivedClass()
{ m_dataInDerived = 0; }
public void SetDataDerived( int val ) { m_data = val; }
override public void ClearData()
{
base.ClearData(); // I WANT TO REINFORCE THIS CALL OR AUTOMATE THIS
IN THE BaseClass
m_dataInDerived = 0;
}
}
Nov 15 '05 #1
4 1245
You can always make the base method not virtual and have it call a virtual
method after it's done whatever needs to be done first in the base..

Niall

"Zeng" <zz*@nonospam.com> wrote in message
news:uZ*************@tk2msftngp13.phx.gbl...

I often run into situation where I would like to have a method such as that has derived behavior similar to destructors in c++, is that possible?

public BaseClass
{
private int m_dataInBase;
public BaseClass()
{m_dataInBase = 0}
public void SetDataBase( int val ) { m_data = val; }
virtual public void ClearData()
{
m_dataInBase = 0;
}
}

public DerivedClass
{
private int m_dataInDerived;
public DerivedClass()
{ m_dataInDerived = 0; }
public void SetDataDerived( int val ) { m_data = val; }
override public void ClearData()
{
base.ClearData(); // I WANT TO REINFORCE THIS CALL OR AUTOMATE THIS IN THE BaseClass
m_dataInDerived = 0;
}
}

Nov 15 '05 #2
On Wed, 17 Sep 2003 18:52:56 -0700, "Zeng" <zz*@nonospam.com> wrote:

I often run into situation where I would like to have a method such as that
has derived behavior similar to destructors in c++, is that possible?

public BaseClass
{
private int m_dataInBase;
public BaseClass()
{m_dataInBase = 0}
public void SetDataBase( int val ) { m_data = val; }
virtual public void ClearData()
{
m_dataInBase = 0;
}
}

public DerivedClass
{
private int m_dataInDerived;
public DerivedClass()
{ m_dataInDerived = 0; }
public void SetDataDerived( int val ) { m_data = val; }
override public void ClearData()
{
base.ClearData(); // I WANT TO REINFORCE THIS CALL OR AUTOMATE THIS
IN THE BaseClass
m_dataInDerived = 0;
}
}

Overriding means that you 'know how to handle the thing better' than
the base class...
So you will have to manually call base.xxx();

You might want to take a look at the Dispose pattern...

Linebreaks!
http://msdn.microsoft.com/library/de...izedispose.asp
--
NULL
Nov 15 '05 #3
that won't work if the derivation hierarchy is bigger. In C++ gets called,
the destructors from the leaf class to the base class in the hierarchy get
called automatically.

"Niall" <as**@me.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
You can always make the base method not virtual and have it call a virtual
method after it's done whatever needs to be done first in the base..

Niall

"Zeng" <zz*@nonospam.com> wrote in message
news:uZ*************@tk2msftngp13.phx.gbl...

I often run into situation where I would like to have a method such as

that
has derived behavior similar to destructors in c++, is that possible?

public BaseClass
{
private int m_dataInBase;
public BaseClass()
{m_dataInBase = 0}
public void SetDataBase( int val ) { m_data = val; }
virtual public void ClearData()
{
m_dataInBase = 0;
}
}

public DerivedClass
{
private int m_dataInDerived;
public DerivedClass()
{ m_dataInDerived = 0; }
public void SetDataDerived( int val ) { m_data = val; }
override public void ClearData()
{
base.ClearData(); // I WANT TO REINFORCE THIS CALL OR AUTOMATE

THIS
IN THE BaseClass
m_dataInDerived = 0;
}
}


Nov 15 '05 #4
But that only happens for destructors in C++, the virtual and pure virtual
methods don't follow that behaviour. As NULL said, when you override, your
basically declaring that the base class doesn't understand how to operate
properly in this context. If there was a type of virtual that forced the
base call, it could become very annoying if you are trying to override the
method so that you can set up some data before the base does its thing.

Just as an aside, aren't you talking about constructos as the analogy here,
not destructors? Constructors run base->derived, which is what you're
describing.

"Zeng" <zz*@nonospam.com> wrote in message
news:OM**************@TK2MSFTNGP12.phx.gbl...
that won't work if the derivation hierarchy is bigger. In C++ gets called, the destructors from the leaf class to the base class in the hierarchy get
called automatically.

"Niall" <as**@me.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
You can always make the base method not virtual and have it call a virtual method after it's done whatever needs to be done first in the base..

Niall

"Zeng" <zz*@nonospam.com> wrote in message
news:uZ*************@tk2msftngp13.phx.gbl...

I often run into situation where I would like to have a method such as

that
has derived behavior similar to destructors in c++, is that possible?

public BaseClass
{
private int m_dataInBase;
public BaseClass()
{m_dataInBase = 0}
public void SetDataBase( int val ) { m_data = val; }
virtual public void ClearData()
{
m_dataInBase = 0;
}
}

public DerivedClass
{
private int m_dataInDerived;
public DerivedClass()
{ m_dataInDerived = 0; }
public void SetDataDerived( int val ) { m_data = val; }
override public void ClearData()
{
base.ClearData(); // I WANT TO REINFORCE THIS CALL OR AUTOMATE

THIS
IN THE BaseClass
m_dataInDerived = 0;
}
}



Nov 15 '05 #5

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

Similar topics

14
by: tertius | last post by:
Is there a better way to append certain chars in a string with a backslash that the example below? chr = "#$%^&_{}" # special chars to look out for str = "123 45^ & 00 0_" # string to...
20
by: Raymond Lewallen | last post by:
I read this on this website page http://www.vbip.com/books/1861004915/chapter_4915_06.asp: Unlike many object-oriented languages, all methods in VB.NET are virtual. Now in BOL, Under...
7
by: al | last post by:
class Base { public: virtual void method(); }; class Derive : public Base { public: void method();
8
by: Pierre Couderc | last post by:
I am looking for a "special" kind of map : - it is read like a map - if the searched element exists, it is given back imediately - if the searched element does not exist, an initialise() is...
32
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
14
by: JPRoot | last post by:
Hi I use the following syntax to have events inherited from base to child classes which works nicely (virtual and override keyword on events). But I am wondering if it is a "supported" way of using...
175
by: Ken Brady | last post by:
I'm on a team building some class libraries to be used by many other projects. Some members of our team insist that "All public methods should be virtual" just in case "anything needs to be...
11
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the...
0
by: akshaycjoshi | last post by:
I am reading a book which says Even though unboxed value types don't have a type object pointer, you can still call virtual methods (such as Equals, GetHashCode, or ToString) inherited or...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.