473,568 Members | 2,882 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1259
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.c om> wrote in message
news:uZ******** *****@tk2msftng p13.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.c om> 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******** ********@TK2MSF TNGP12.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.c om> wrote in message
news:uZ******** *****@tk2msftng p13.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.c om> wrote in message
news:OM******** ******@TK2MSFTN GP12.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******** ********@TK2MSF TNGP12.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.c om> wrote in message
news:uZ******** *****@tk2msftng p13.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
24069
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 convert n = "" # init new string for i in str: if i in chr: # if special character in str n+='\\' # append...
20
1756
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 Perforamce Tips and Tricks in .NET Applications, A .Net Developer Platform White Paper, at the very bottom, it says: The JIT cannot inline virtual methods,...
7
1402
by: al | last post by:
class Base { public: virtual void method(); }; class Derive : public Base { public: void method();
8
1467
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 called to do the job. - anyway the map has a limited size, when it is full, the oldest element is dropped ( if recalled later, will need again an...
32
4484
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
14
12111
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 events since I never saw it used anywhere in MSDN documentation/samples?! Or it will just break when I upgrade to .NET Framework 2.x in the coming...
175
8730
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 changed". This is very much against my instincts. Can anyone offer some solid design guidelines for me? Thanks in advance....
11
3403
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 vtable pointer is made use of.. eg. class one {
0
1793
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 overridden by the type. The reason is because the CLR can just call these methods nonvirtually and System.ValueType overrides all of these virtual methods...
0
7693
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...
0
7604
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8117
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...
1
7660
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...
0
7962
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...
1
5498
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...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
932
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...

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.