473,383 Members | 1,952 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,383 software developers and data experts.

Virtual functions, calling inherited class. Quicky.

I'm having real brain failure today.
I've done this lots of times with constructors but not with virtual methods
and the compiler complains because Ive put the :base(foo) after the function
header.
I want to call the BaseResult class to let it write shared data before I let
the derived class write its own data. What am I doing wrong please.

public abstract class BaseResult

{

public virtual void Write(System.IO.BinaryWriter binarywriter)

{

WriteMyData()

}

}

public class MimicResult : BaseResult

{

public override void Write(System.IO.BinaryWriter binarywriter) :
base(binarywriter) /*compiler complains*/

{

// Do I do something here?
}

}
Nov 16 '05 #1
4 1610
Claire,

The "SubClass() : BaseClass()" syntax is used only for constructors. It's a
way to indicate which base class constructor to use before entering the
subclass constructor, which is necessary since the base class is
instantiated before any subclass code runs (including its constructor).

For methods, you can simply call the base class method from within the
subclass method. e.g.:

public override void Write(System.IO.BinaryWriter binarywriter)
{
base(binarywriter);
}

You can run other code before/after/instead of the base method call as
desired.

HTH,
Nicole

"Claire" <bl****@blahhhhh.com> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
I'm having real brain failure today.
I've done this lots of times with constructors but not with virtual
methods
and the compiler complains because Ive put the :base(foo) after the
function
header.
I want to call the BaseResult class to let it write shared data before I
let
the derived class write its own data. What am I doing wrong please.

public abstract class BaseResult

{

public virtual void Write(System.IO.BinaryWriter binarywriter)

{

WriteMyData()

}

}

public class MimicResult : BaseResult

{

public override void Write(System.IO.BinaryWriter binarywriter) :
base(binarywriter) /*compiler complains*/

{

// Do I do something here?
}

}

Nov 16 '05 #2
Claire,

The "SubClass() : BaseClass()" syntax is used only for constructors. It's a
way to indicate which base class constructor to use before entering the
subclass constructor, which is necessary since the base class is
instantiated before any subclass code runs (including its constructor).

For methods, you can simply call the base class method from within the
subclass method. e.g.:

public override void Write(System.IO.BinaryWriter binarywriter)
{
base.Write(binarywriter);
}

You can run other code before/after/instead of the base method call as
desired.

HTH,
Nicole
"Claire" <bl****@blahhhhh.com> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
I'm having real brain failure today.
I've done this lots of times with constructors but not with virtual
methods
and the compiler complains because Ive put the :base(foo) after the
function
header.
I want to call the BaseResult class to let it write shared data before I
let
the derived class write its own data. What am I doing wrong please.

public abstract class BaseResult

{

public virtual void Write(System.IO.BinaryWriter binarywriter)

{

WriteMyData()

}

}

public class MimicResult : BaseResult

{

public override void Write(System.IO.BinaryWriter binarywriter) :
base(binarywriter) /*compiler complains*/

{

// Do I do something here?
}

}

Nov 16 '05 #3
Claire wrote:
I'm having real brain failure today.
I've done this lots of times with constructors but not with virtual methods
and the compiler complains because Ive put the :base(foo) after the function
header.
I want to call the BaseResult class to let it write shared data before I let
the derived class write its own data. What am I doing wrong please.

public abstract class BaseResult

{

public virtual void Write(System.IO.BinaryWriter binarywriter)

{

WriteMyData()

}

}

public class MimicResult : BaseResult

{

public override void Write(System.IO.BinaryWriter binarywriter) :
base(binarywriter) /*compiler complains*/

{

// Do I do something here?
}

}


Yes :)

public override void Write(...)
{
base.Write(...)
etc()
}

David Logan
Nov 16 '05 #4
Thanks David and Nicole,
think I'll have an early night tonight

Nov 16 '05 #5

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

Similar topics

99
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less...
4
by: vijay | last post by:
I have a doubt with size of classed with virtual functions I have declared A,A1,A2 ,B , C, D some classes with no varaibles but a vitual function each, The size of A is as expected 4 bytes with...
5
by: Ryan Faulkner | last post by:
Hi, Im having a few problems with virtual functions (Im using the Visual C++ environment by the way). I have a base class with three virtual functions and a derived class with a single new...
9
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 {
6
by: Alden Pierre | last post by:
Hello, http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.7 As per the link above it's wise to have a virtual deconstructor when creating an abstract class. Here is when I'm...
3
by: kikazaru | last post by:
Is it possible to return covariant types for virtual methods inherited from a base class using virtual inheritance? I've constructed an example below, which has the following structure: Shape...
7
by: Markus Svilans | last post by:
Hello, My question involves virtual functions and inheritance. Suppose we have a class structure, that consists of "data" classes, and "processor" classes. The data classes are derived from...
23
by: Dave Rahardja | last post by:
Since C++ is missing the "interface" concept present in Java, I've been using the following pattern to simulate its behavior: class Interface0 { public: virtual void fn0() = 0; };
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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: 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...

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.