473,385 Members | 1,587 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,385 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 1611
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...
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: 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:
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: 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
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
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...
0
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,...
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.