473,783 Members | 2,269 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Operators Inherited?

Hi all.

I was led to believe that static methods were not inherited by their
subclasses (and since that makes sense, rightly so).

However, a subclass I've written is using it's (abstract) superclass's
operators. How can that be when operators are always static?

Is it a special case? It's handy, sure, but doesn't it "break" the language
somehow? Or isn't this actually inheritance at play here?

Shak
Aug 2 '06 #1
5 1281
Shak wrote:
I was led to believe that static methods were not inherited by their
subclasses (and since that makes sense, rightly so).
Huh? Why would you think that? The only thing special about static
methods is that they don't get a "this" reference. Public and internal
static members are just as visible to derived classes as they are to
totally unrelated classes; protected static members are just as
visible to derived classes as protected instance members.

--

..NET 2.0 for Delphi Programmers www.midnightbeach.com/.net
Delphi skills make .NET easy to learn In print, in stores.
Aug 2 '06 #2
"Shak" <me@privacy.net wrote:
I was led to believe that static methods were not inherited by their
subclasses (and since that makes sense, rightly so).
They can't be overridden, so they're not inherited in the same way as
virtual methods are, but the whole namespace of the ancestor type is
merged into the descendant type, so there is a kind of inheritance of
scope.
However, a subclass I've written is using it's (abstract) superclass's
operators. How can that be when operators are always static?
When you use an operator on two custom types, the compiler examines both
types' scopes, looking for operator overload definitions. So, if you've
got two types, Foo and Bar, with values foo and bar:

foo Op bar

.... it'll look in the namespaces of both Foo and Bar for static methods
called op_Op (where 'Op' is Add, Subtract, etc.) for which there exist
overloads that can accept values of type (Foo, Bar). That 'accept'
includes polymorphism by way of inheritance, of course.

Now, static methods don't get inherited in the sense of a virtual
instance method - but because the whole namespace is inherited, all the
public static methods that are visible in the ancestor are also visible
in the descendant. Thus, the ancestor's operators also work on its
descendants.

-- Barry

--
http://barrkel.blogspot.com/
Aug 2 '06 #3
"Jon Shemitz" <jo*@midnightbe ach.comwrote in message
news:44******** *******@midnigh tbeach.com...
Shak wrote:
>I was led to believe that static methods were not inherited by their
subclasses (and since that makes sense, rightly so).

Huh? Why would you think that? The only thing special about static
methods is that they don't get a "this" reference. Public and internal
static members are just as visible to derived classes as they are to
totally unrelated classes; protected static members are just as
visible to derived classes as protected instance members.

Perhaps "inherited" was the wrong word. What I meant was that if a
superclass has a public static void method GetInt(), you would not be able
to call that method on a subclass - ie you can't mark them virtual.

My initial impression was that operators were just convienince for regular
static methods. So:

public static bool operator==(A one, A two), was equivalent to

public static bool myEquals(A one, A two).

They're not though, since, where B is a subclass of A:

B b1 = new B();
B b2 = new B();

bool result = (b1==b2); //compiles and uses the impl of operator== in A,
bool result2 = b1.Equals(b2); //doesn't compile, obviously.

I've since read after posting the OP that operators aren't just static
methods, and further what is to be called gets determined at compile time.
So, not only do you get access to any operators in the superclass, but you
may even get to use an operator that hasn't been explicity declared for your
type (like above with b1 == b2), which doesn't occur with regular virtual
methods (unless you cast).

Shak
Aug 3 '06 #4
"Barry Kelly" <ba***********@ gmail.comwrote in message
news:gi******** *************** *********@4ax.c om...
"Shak" <me@privacy.net wrote:
>I was led to believe that static methods were not inherited by their
subclasses (and since that makes sense, rightly so).

They can't be overridden, so they're not inherited in the same way as
virtual methods are, but the whole namespace of the ancestor type is
merged into the descendant type, so there is a kind of inheritance of
scope.
Is that just the case for operators?
>However, a subclass I've written is using it's (abstract) superclass's
operators. How can that be when operators are always static?

When you use an operator on two custom types, the compiler examines both
types' scopes, looking for operator overload definitions. So, if you've
got two types, Foo and Bar, with values foo and bar:

foo Op bar

... it'll look in the namespaces of both Foo and Bar for static methods
called op_Op (where 'Op' is Add, Subtract, etc.) for which there exist
overloads that can accept values of type (Foo, Bar). That 'accept'
includes polymorphism by way of inheritance, of course.
Again, is this just the case for operators?
Now, static methods don't get inherited in the sense of a virtual
instance method - but because the whole namespace is inherited, all the
public static methods that are visible in the ancestor are also visible
in the descendant. Thus, the ancestor's operators also work on its
descendants.
But if that's so, why can't you call a superclass's static method from its
subclass (like I try to do in my other post)?

Shak
Aug 3 '06 #5
"Shak" <me@privacy.net wrote in message
news:4j******** ****@individual .net...
"Barry Kelly" <ba***********@ gmail.comwrote in message
news:gi******** *************** *********@4ax.c om...
>"Shak" <me@privacy.net wrote:
>Now, static methods don't get inherited in the sense of a virtual
instance method - but because the whole namespace is inherited, all the
public static methods that are visible in the ancestor are also visible
in the descendant. Thus, the ancestor's operators also work on its
descendants.

But if that's so, why can't you call a superclass's static method from its
subclass (like I try to do in my other post)?
Ooops, seems like you can after all and I'm talking rubbish! My mistake...
Disregard.

Thanks for the help guys!

Shak
Aug 3 '06 #6

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

Similar topics

14
2531
by: greg | last post by:
Discussion is invited on the following proto-PEP. ------------------------------------------------------------- PEP ??? - Overloadable Boolean Operators ======================================== SUMMARY This PEP proposes an extension to permit objects to define their own
6
3665
by: Christian Brechbühler | last post by:
The template std::valarray behaves pretty much like a mathematical vector. Arithmetic operators apply elementwise. Now I'd like to extend this to a user-defined type, e.g., complex. Multiplying a double by a complex number gives a complex number. Now I would like to multiply a valarray of doubles by a complex number and get a valarray of complex numbers. Of course, a C++ compiler won't jump to that conclusion; I need to define...
5
6887
by: Andy Jarrell | last post by:
I'm trying to inherit from a specific class that has an overloaded operator. The problem I'm getting is that certain overloaded operators don't seem to come with the inheritance. For example: // TestA.h --------------------------------------- #include <iostream> enum Aval { FIRST_VALUE,
6
2023
by: Raf256 | last post by:
Hi, I would extend class only a bit. In example: I want to write my own class, that works all like std::string, only it also have member int mDatabase; and it have method SaveToDatabase(); I could do like: class cStorableStr : std::string { private: int mDatabase;
25
2585
by: tsaar2003 | last post by:
Hi Pythonians, To begin with I'd like to apologize that I am not very experienced Python programmer so please forgive me if the following text does not make any sense. I have been missing constants in Python language. There are some workarounds available, for example the const-module. To me, this looks quite cumbersome and very unintuitive. For the interpreter, in the efficiency-wise, I just cannot tell.
1
1307
by: Dave Rudolf | last post by:
Hey all, The best way to explain my problem is with an example, so here goes: I have a class that I use to represent a numeric vector (i.e., a point in some multi-dimensional space). The class has operators for assignment, addition, etc. So, for example, the essentials of the class are class NumericVector
1
1533
by: Kambehba | last post by:
Would you please tell me that if the Base class constructors and assignment operators are inherited by the derived classes or not. and any more information that apply. Thank you. kambiz
3
1366
by: Noodle | last post by:
Hi Guys, not sure if anyone can help me on this one but...... I have a class that I overload the equality operator (=). I also have a class that inherits from this first class which has extra attributes that i need to check to see if they are equal for the previous overload to work correctly. No my question is how do I do this? The obvious answer is to overload the overloaded operator and then copy the comparison routine from the...
5
2656
by: puzzlecracker | last post by:
I don't recall whether operators, which are members of the class, are intherited in subclasses? thanks
0
9480
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
10313
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...
1
10081
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9946
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...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6735
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4044
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
3643
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.