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

Problem with overloaded functions

Hello,

I get warnings when trying to compile this code:

public class Point3D : Point2D
{
public void WriteXml(XmlWriter w)
{
// TODO
}

public XmlSchema GetSchema()
{
// TODO
return null;
}

public void ReadXml2(XmlReader reader)
{
// TODO
}
}

public class Point2D : IXmlSerializable
{
public void WriteXml(XmlWriter w)
{
}

public XmlSchema GetSchema()
{
return null;
}

public void ReadXml(XmlReader reader)
{
}
}

class1.cs(23,15): warning CS0108: The keyword new is required on
'Point3D.WriteXml(System.Xml.XmlWriter)' because it hides inherited member
'Point2D.WriteXml(System.Xml.XmlWriter)'
class1.cs(65,15): (Related location)
class1.cs(32,20): warning CS0108: The keyword new is required on
'Point3D.GetSchema()' because it hides inherited member
'Point2D.GetSchema()'
class1.cs(73,20): (Related location)
class1.cs(38,15): warning CS0108: The keyword new is required on
'Point3D.ReadXml(System.Xml.XmlReader)' because it hides inherited member
'Point2D.ReadXml(System.Xml.XmlReader)'
class1.cs(79,15): (Related location)

Why can I not overload functions this way? Point3D.WriteXml is not called
when I think it should be. Can someone explain this to a C++ programmer?
Thanks!

--
Daniel
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 16 '05 #1
4 3038

You should declare the overridable methods in the Point2D
as virtual to make them overridable(or abstract if they must be overridden,
and have no default inplementation).

Then use keyword override in the derived class Point3D,
like this:
public class Point3D : Point2D
{
public override void WriteXml(XmlWriter w)
{
// TODO
}

public override XmlSchema GetSchema()
{
// TODO
return null;
}

public void ReadXml2(XmlReader reader)
{
// TODO
}
}

public class Point2D : IXmlSerializable
{
public virtual void WriteXml(XmlWriter w)
{
}

public virtual XmlSchema GetSchema()
{
return null;
}

public void ReadXml(XmlReader reader)
{
}
}
--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Daniel Lidström" <so*****@microsoft.com> wrote in message
news:6u****************************@40tude.net...
Hello,

I get warnings when trying to compile this code:

public class Point3D : Point2D
{
public void WriteXml(XmlWriter w)
{
// TODO
}

public XmlSchema GetSchema()
{
// TODO
return null;
}

public void ReadXml2(XmlReader reader)
{
// TODO
}
}

public class Point2D : IXmlSerializable
{
public void WriteXml(XmlWriter w)
{
}

public XmlSchema GetSchema()
{
return null;
}

public void ReadXml(XmlReader reader)
{
}
}

class1.cs(23,15): warning CS0108: The keyword new is required on
'Point3D.WriteXml(System.Xml.XmlWriter)' because it hides inherited member
'Point2D.WriteXml(System.Xml.XmlWriter)'
class1.cs(65,15): (Related location)
class1.cs(32,20): warning CS0108: The keyword new is required on
'Point3D.GetSchema()' because it hides inherited member
'Point2D.GetSchema()'
class1.cs(73,20): (Related location)
class1.cs(38,15): warning CS0108: The keyword new is required on
'Point3D.ReadXml(System.Xml.XmlReader)' because it hides inherited member
'Point2D.ReadXml(System.Xml.XmlReader)'
class1.cs(79,15): (Related location)

Why can I not overload functions this way? Point3D.WriteXml is not called
when I think it should be. Can someone explain this to a C++ programmer?
Thanks!

--
Daniel
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Nov 16 '05 #2
The 3D class is derived from the 2D class. The XmlWriter method is not
marked as virtual so overloads must have a different parameter signature or
the method will replace that of the base class. If the intention is to
replace the base method use the new keyword on the derived class, if the
intent is to override the method use the virtual keyword on the base class.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"Daniel Lidström" <so*****@microsoft.com> wrote in message
news:6u****************************@40tude.net...
Hello,

I get warnings when trying to compile this code:

public class Point3D : Point2D
{
public void WriteXml(XmlWriter w)
{
// TODO
}

public XmlSchema GetSchema()
{
// TODO
return null;
}

public void ReadXml2(XmlReader reader)
{
// TODO
}
}

public class Point2D : IXmlSerializable
{
public void WriteXml(XmlWriter w)
{
}

public XmlSchema GetSchema()
{
return null;
}

public void ReadXml(XmlReader reader)
{
}
}

class1.cs(23,15): warning CS0108: The keyword new is required on
'Point3D.WriteXml(System.Xml.XmlWriter)' because it hides inherited member
'Point2D.WriteXml(System.Xml.XmlWriter)'
class1.cs(65,15): (Related location)
class1.cs(32,20): warning CS0108: The keyword new is required on
'Point3D.GetSchema()' because it hides inherited member
'Point2D.GetSchema()'
class1.cs(73,20): (Related location)
class1.cs(38,15): warning CS0108: The keyword new is required on
'Point3D.ReadXml(System.Xml.XmlReader)' because it hides inherited member
'Point2D.ReadXml(System.Xml.XmlReader)'
class1.cs(79,15): (Related location)

Why can I not overload functions this way? Point3D.WriteXml is not called
when I think it should be. Can someone explain this to a C++ programmer?
Thanks!

--
Daniel
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Nov 16 '05 #3
Daniel Lidström <so*****@microsoft.com> wrote:

<snip>
Why can I not overload functions this way? Point3D.WriteXml is not called
when I think it should be. Can someone explain this to a C++ programmer?


You haven't overloaded any methods - you've hidden them. Overloading
occurs when there are multiple methods with the same name but different
signatures.

Were you actually intending to override the methods? If so, you need to
declare that they're virtual in the base class, and that you're
overriding them in the child class.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
Thanks for clearing this up, guys!

--
Daniel
Nov 16 '05 #5

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

Similar topics

3
by: Cindy Liu | last post by:
Hi Everyone, I created C# COM+ component. It has two overloaded methods - the method names are same and their signatures are different, one takes two parameters and another takes four. I coded...
8
by: Nitin Bhardwaj | last post by:
Thanx in advance for the response... I wanna enquire ( as it is asked many a times in Interviews that i face as an Engg PostGraduate ) about the overloading capability of the C++ Language. ...
4
by: August1 | last post by:
I've written an interface and implementation file along with a client source file that allows the use of an overloaded subtraction operator. However, when using the program, I'm running into a...
4
by: Leslaw Bieniasz | last post by:
Cracow, 20.09.2004 Hello, I need to implement a library containing a hierarchy of classes together with some binary operations on objects. To fix attention, let me assume that it is a...
44
by: bahadir.balban | last post by:
Hi, What's the best way to implement an overloaded function in C? For instance if you want to have generic print function for various structures, my implementation would be with a case...
3
by: cybertof | last post by:
Hello, Is it possible in C# to have 2 overloaded functions with - same names - same parameters - different return type values If no, is it possible in another language ?
10
by: Grizlyk | last post by:
1. Can abybody explain me why C++ function can not be overloaded by its return type? Return type can has priority higher than casting rules. For example: char input(); //can be compiled to name...
16
by: EM.Bateman | last post by:
Working on Visual Studio .Net I've implemented a class: #ifndef CONTRIBUTOR_H #define CONTRIBUTOR_H enum Gender {male=1, female, unk}; #include <iostream> #include <iomanip> #include...
2
by: desktop | last post by:
If a function should work with different types you normally overload it: void myfun(int a){ // do int stuff } void myfun(std::string str){ // do string stuff }
4
by: king kikapu | last post by:
Hi, i am trying, to no avail yet, to take a C#'s overloaded functions skeleton and rewrite it in Python by using closures. I read somewhere on the net (http://dirtsimple.org/2004/12/python-is-...
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: 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
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
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.