473,769 Members | 1,917 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1301

"Daniel Billingsley" <db**********@N O.durcon.SPAAMM .com> wrote in message
news:e9******** ******@TK2MSFTN GP11.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.WriteLi ne("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.WriteLi ne("B.F"); }
}

class C: B
{
public override void F() { Console.WriteLi ne("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.mi crosoft.com> wrote in message
news:Ts******** ******@cpmsftng xa07.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
1424
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 constructor would always need to be called before anything else seeing as the subclass may well depend on functionality and variables created in the base class.
20
2890
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
5200
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
2404
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
2249
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 me. So, I'm here for an answer (more of a confirmation), hopefully. First let me say that I know people keep saying; it doesn't work because the member "is a private". I believe there's more to it than just simply that... Theory: You inherit,...
0
2834
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 magic, I wrote the following module. It is mainly useful as a light weight tool to help programmers catch mistakes at definition time (e.g., forgetting to implement a method required by the given interface). This is handy when unit tests or...
26
5375
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 because I am not always able to control/create all the different constructors the base class has. My problem can be described in code as follows ... /* This is the base class with a whole heap of constructors/functionality*/ public class Animal
3
1623
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 work here)? The conversion is done for this purpose: I have an std::map<std::string, Base*>. I want to "associate" Derived* handles to the stored Base* so when Base* in the map changes (ie points another address), the Derived* handle outside of...
19
2236
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 modify come of the classes so they match our purpose better - mostly add a few methods etc. Example: Open source lib has classes Map and Layer defined. The relationship between them is one to many. We created our own versions (inherited) of Map...
15
3535
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 you want to make a deque which can contain any objects of any of those types. Normally what you would have to do is to make a deque or vector of pointers of the base class type and then allocate each object dynamically with 'new' and store the...
0
9579
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9420
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10205
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9851
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7401
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3949
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3556
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2811
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.