LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-06-2007, 04:53 PM   #1
PatrickNew
Senior Member
 
Registered: Jan 2006
Location: Charleston, SC, USA
Distribution: Debian, Gentoo, Ubuntu, RHEL
Posts: 1,148
Blog Entries: 1

Rep: Reputation: 48
C++ typeid and static methods


I know that I can compare the type of objects at runtime by using typeid(). This provides a reference to an object representing the class of the object. What I want to know is, is their any way to call a static method off of the Type object?

Ultimately, what I would like is a polymorphic static function. That is, I have an abstract class which provides a couple of methods which really ought to be static. Yet, I would like the classes which inherit from this class to override these methods.

I would love it if I could do something like this:

Code:
typeid(obj)::static_method()
But this doesn't compile.

According to some online documentation, there is an extension to IBM compilers that will let you do this with the 'typeof' operator, but I'd rather not be tied to a particular compiler.

Alternatively, I could just implement them as regular methods, but I'd hate to have all that unnecessary overhead. So, does anyone have an idea? Can it be done?
 
Old 10-07-2007, 01:05 PM   #2
PatrickNew
Senior Member
 
Registered: Jan 2006
Location: Charleston, SC, USA
Distribution: Debian, Gentoo, Ubuntu, RHEL
Posts: 1,148

Original Poster
Blog Entries: 1

Rep: Reputation: 48
Okay, after revisiting the design of the program, I guess it does make more sense for these to be regular methods. But I guess if anyone knows the answer to the original question it might still be nice to know, just for curiosity/academic reasons.
 
Old 10-07-2007, 08:10 PM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
I don't think this is possible in C++, and I'm not sure why writing
Code:
typeid(obj)::method()
is better than
Code:
obj.method()
Seems like the first way is just more typing with no extra benefit. If you think it would somehow be more efficient, remember that you are still asking for a runtime dispatch so calling it a static method doesn't automagically reduce overhead.
 
Old 10-07-2007, 09:05 PM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
I don't think that there is an easy way around your requirement, although I'm not certain what "unnecessary overhead" you are concerned about.

There is an approach that I can think about:
In each class you have two methods the static and the non-static method with the non-static method defined as virtual and it calls the classes static method. This means that you can call the static method directly if you know which class you want to fire the method, otherwise if you have an object you can use the non-static method to search the virtual tables to determine which method should be called.
 
Old 10-07-2007, 09:22 PM   #5
PatrickNew
Senior Member
 
Registered: Jan 2006
Location: Charleston, SC, USA
Distribution: Debian, Gentoo, Ubuntu, RHEL
Posts: 1,148

Original Poster
Blog Entries: 1

Rep: Reputation: 48
Upon researching more, my idea of "unnecessary overhead" was due to my misunderstanding of how c++ implements objects. In my education, I had been told that we could think of their being a separate copy of a method existing for each object, and that only a single copy of a static method existed. I realize now that it only appears that way to the programmer, but object code is not actually being copied about - only one copy of method() exists, and c++ just sets up the this pointer. I realize now that instantiating an object does not involve copying the memory space of the methods.

I further realize my my desire to have polymorphic static methods was really just a product of a bad design - static methods just aren't meant to be polymorphic.
 
Old 10-10-2007, 07:16 AM   #6
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
That is correct. There is only one copy of each function but member functions implicitly take an additional argument which is the this pointer. The compiler inserts this argument when compiling the function and passes its value to the function based on the left side of the . or -> (plus any conversions required to facilitate polymorphism.) It is still valid to call a static function as if it were a member function; it just eliminates inserting a this argument.
ta0kira
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Howto Assign Multiple Static Public IP Addresses under SBC's PPPoE Static Ip system o trekgraham Linux - Networking 8 04-17-2007 10:51 AM
Java: non-static variable this cannot be referenced from a static context chief_officer Programming 4 05-29-2006 01:37 PM
typeid(x).name() question neo_in_matrix Programming 3 06-08-2005 05:44 PM
Is there any static ARP for static IP network? linux_lover2005 Linux - Networking 2 05-18-2005 12:01 PM
FC2 Overriding static if in favor of dhcp system set for static pkraus109 Linux - Networking 8 09-21-2004 11:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration