How to access a method without creating a class object

I have a class BearingX which inherits from a class named Domain. Domain has a private variable "time" and a public method getCurrentTime() which returns the private variable "time".
I need to access this method getCurrentTime() or variable "time" in my class BearingX without creating an object of class Domain. Is it possible, if yes, then how?
If BearingX is derived from Domain, then a BearingX object "IS A" Domain object. (That's why inheritance is often said to represent an "IS A" relationship). You can treat getCurrentTime() as if it's a member of your BearingX class (because it is).

Note - if you've overridden getCurrentTime with your own implementation in BearingX, then to access the version from Domain, you would need to use Domain::getCurrentTime() when calling the function, otherwise you can omit the Domain:: bit
Last edited on
I am sorry for confusion, I confused inheritance with preprocessor directives. My bad!
Okay, so my BearingX doesn't inherit from class Domain. It includes the file like #include <domain.h>.
So now, is there a way to access getCurrentTime() now?
No. there is no such a trick, because getCurrentTime has implicit object parameter.
You could do what you want if getCurrentTine were a static method.
Last edited on
You need to create static public getCurrentTime() method in Domain class. time variable also must be static:

class Domain {
static time;
static getCurrentTime();

You should then access to this method from BearingX class by calling Domain::getCurentTime().

If you need, you can find on Google many articles about about c++ static functions.
Thank you guys for help.
I finally had to define a method which returned me current domain in the analysis and then got access to getCurrentTime() method:

Topic archived. No new replies allowed.