473,385 Members | 1,720 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.

ok to use base. when I don't have to?

I understand the conditions where you have to use base., but does it make
any sense to use it elsewhere to make clear to the reader that the method or
property you refer to comes from the base class? Or, would you find such
code insulting or clutter since you're smart enough to see that the class is
inherited?
Nov 15 '05 #1
6 1287

"Daniel Billingsley" <db**********@NO.durcon.SPAAMM.com> wrote in message
news:e9**************@TK2MSFTNGP11.phx.gbl...
I understand the conditions where you have to use base., but does it make
any sense to use it elsewhere to make clear to the reader that the method or property you refer to comes from the base class? Or, would you find such
code insulting or clutter since you're smart enough to see that the class is inherited?
I would suggest against it, if for no reason other than during development
and design you would lock yourself into using a non-virtual method or
property. In the case of a virtual member you would circumvent your
polymorphism and call the base class specifically.
Use it only when you *need* to call the base class, not whenver you are
calling an inherited method

Nov 15 '05 #2
n!
> I understand the conditions where you have to use base., but does it make
any sense to use it elsewhere to make clear to the reader that the method or property you refer to comes from the base class? Or, would you find such
code insulting or clutter since you're smart enough to see that the class is inherited?


I would say 'no it doesn't make sense to use it elsewhere'. Mainly for
versioning issues, if the method in the base class is non virtual, simply
calling the method allows for the method to be made virtual and overridden
in the future without having to remember 'oh yeh, I need to change that base
call'. IMHO in a OO world you don't need to know whether the method is being
called in a base class or not unless you *really* need to know (ie. you want
to call the base implementation even though you have an overridden version).

n!
Nov 15 '05 #3

Hi Daniel,

Thank you for posting in the community!

Based on my understanding, you want to know the difference between *use* or
*not use* base keyword to invoke method in C#.

================================================== =======
Actually, if you explicit use base keyword, it means that you want to
invoke the BASE class's method. While "not use" the base keyword, you just
invoke your class's method, which may inherited from your BASE class.

So, the difference may expose using polymorphism, below is the sample code:

class A
{
public virtual void F() { Console.WriteLine("A.F"); }
}

class B: A
{
static void Main()
{
B b=new C();
b.test();
}

public void test()
{
base.F();
F();
}
public override void F() { Console.WriteLine("B.F"); }
}

class C: B
{
public override void F() { Console.WriteLine("C.F"); }
}

The output will generate:
A.F
C.F

So I think you have seen the difference, *not use* base keyword will lead
to the polymorphism, while *use* it will lead to explicit invoke base class
method.

================================================== =====
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.
Have a nice day!!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #4

Hi Daniel,

Does my reply make sense to you?

If you still have anything unclear, please feel free to post, I will help
you.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #5
Yes, the replies made sense.

""Jeffrey Tan[MSFT]"" <v-*****@online.microsoft.com> wrote in message
news:Ts**************@cpmsftngxa07.phx.gbl...

Hi Daniel,

Does my reply make sense to you?

If you still have anything unclear, please feel free to post, I will help
you.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #6

Oh, thanks very much for your feedback, Daniel.

I am glad to hear your feedback that it makes sense :-)

If you have any further concern, please feel free to post, we will help you.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 15 '05 #7

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

Similar topics

8
by: Simon | last post by:
Hi everyone, Quick question: If I don't use base() in a subclass's construcor, will the base classes constructor be called at any point. It's just, I would have thought that the base class...
20
by: modemer | last post by:
Question is as in subject. For example: class BaseClass { public: void func() { do something; } // I don't want this function being overloaded in its inherited class };
9
by: Sean Kirkpatrick | last post by:
To my eye, there doesn't seem to be a whole lot of difference between the two of them from a functional point of view. Can someone give me a good explanation of why one vs the other? Sean
11
by: anongroupaccount | last post by:
What measures should be taken to avoid this sort of thing? class Base { }; class Derived1 : public Base { private: int i, j, k;
7
by: relient | last post by:
Question: Why can't you access a private inherited field from a base class in a derived class? I have a *theory* of how this works, of which, I'm not completely sure of but makes logical sense to...
0
by: emin.shopper | last post by:
I had a need recently to check if my subclasses properly implemented the desired interface and wished that I could use something like an abstract base class in python. After reading up on metaclass...
26
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is...
3
by: Filimon Roukoutakis | last post by:
Dear all, assuming that through a mechanism, for example reflexion, the Derived** is known explicitly. Would it be legal (and "moral") to do this conversion by a cast (probably reinterpret would...
19
by: jan.loucka | last post by:
Hi, We're building a mapping application and inside we're using open source dll called MapServer. This dll uses object model that has quite a few classes. In our app we however need to little bit...
15
by: Juha Nieminen | last post by:
I'm sure this is not a new idea, but I have never heard about it before. I'm wondering if this could work: Assume that you have a common base class and a bunch of classes derived from it, and...
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
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: 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
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.