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

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 1248
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.netwrote:
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*@midnightbeach.comwrote in message
news:44***************@midnightbeach.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.com...
"Shak" <me@privacy.netwrote:
>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.netwrote in message
news:4j************@individual.net...
"Barry Kelly" <ba***********@gmail.comwrote in message
news:gi********************************@4ax.com...
>"Shak" <me@privacy.netwrote:
>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
by: greg | last post by:
Discussion is invited on the following proto-PEP. ------------------------------------------------------------- PEP ??? - Overloadable Boolean Operators...
6
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. ...
5
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: ...
6
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...
25
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...
1
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...
1
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
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...
5
by: puzzlecracker | last post by:
I don't recall whether operators, which are members of the class, are intherited in subclasses? thanks
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: 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: 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.