ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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.
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.
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.
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.