C++ How to invoke an object's method in a different file

  • #1
77
45
TL;DR Summary
In C++, how to run a method from an object that was created in a separate file.
I have created an object for the class Timers in timer_functions.cpp, and I need to run a method associated with that object ( test_method() ) from interrupts.cpp.

I've tried many things but have been unsuccessful. Any ideas would be appreciated. Here is some sample code:

classes.hpp
C++:
   #pragma once
   class Timers {
   public:
     void test_method(void);
   private:
     int testVariable;
   };

classes.cpp
C++:
#include classes.hpp
   void Timers::test_method(void) {
     testVariable++;
   }

timer_functions.cpp
C++:
#include classes.hpp
   Timers myTimerObject; // create the object
interrupts.cpp
C++:
#include classes.hpp
   extern Timers myTimerObject; // this doesn't eliminate the build error
   void timer_interrupt(void) {
     myTimerObject.test_method(); // trying to run the objects method, but BUILD ERROR
   }
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
I'm not clear on what you are trying to do, and how things are split up. Normally, if you have a class foo, there is a header foo.hh and the actual code in foo.cc. If I have code bar.cc, it includes foo.hh, and the linker links foo.o and bar.o.

I don't see how your snippets match to this, but you seem to be creating an instance of an object in one file and using it in another. That shouldn't work - it shouldn't even compile, because the object isn't even declared in the second file. (And if it is declared, how does the compiler know it is supposed to be the same as in the first?)

Normally code would create the object and pass it to other code through a pointer to that object. The same code that created it should destroy it when it is no longer needed,
 
  • #3
Vanadium 50 said:
I'm not clear on what you are trying to do, and how things are split up. Normally, if you have a class foo, there is a header foo.hh and the actual code in foo.cc. If I have code bar.cc, it includes foo.hh, and the linker links foo.o and bar.o.

I don't see how your snippets match to this, but you seem to be creating an instance of an object in one file and using it in another. That shouldn't work - it shouldn't even compile, because the object isn't even declared in the second file. (And if it is declared, how does the compiler know it is supposed to be the same as in the first?)

Normally code would create the object and pass it to other code through a pointer to that object. The same code that created it should destroy it when it is no longer needed,
I don't want to create and destroy the instance for each periodic timer interrupt. While this would probably work, it seems messy.

A pointer to the instance could be created in timer_functions.cpp, but how would it get passed to the timer interrupt?
 
  • #4
Bob Walance said:
I don't want to create and destroy the instance for each periodic timer interrupt. While this would probably work, it seems messy.

A pointer to the instance could be created in timer_functions.cpp, but how would it get passed to the timer interrupt?
If you intend to have only a single (global) instance of Timers, then you can use a "static" instance. In that case, read up on static class variables and static member fns.

Of course, if you do indeed intend that, then why call it "Timers" (plural) instead of "Timer" singular?
 
  • Like
Likes Vanadium 50 and Bob Walance
  • #5
strangerep said:
If you intend to have only a single (global) instance of Timers, then you can use a "static" instance. In that case, read up on static class variables and static member fns.
Wow. That works!

Yes, a single instance of the class (really it's no instance) is just fine for my application.

Thank you for the simple solution.
 

1. How do I invoke an object's method in a different file in C++?

To invoke an object's method in a different file in C++, you will need to first include the header file containing the class definition of the object in your current file using the #include preprocessor directive. Then, you can create an object of that class and call its methods using the dot operator.

2. Can I invoke a private method of an object in a different file in C++?

No, private methods of an object can only be accessed within the class itself. If you need to invoke a private method in a different file, you can either make it a public method or create a public method that calls the private method internally.

3. How can I pass parameters to an object's method when invoking it in a different file?

To pass parameters to an object's method when invoking it in a different file, you can simply include the parameters in the method call using the same data types as specified in the method's definition. Make sure the method is declared with the same parameters in both the header and source files.

4. Is it possible to invoke a static method of an object in a different file in C++?

Yes, you can invoke a static method of an object in a different file in C++. Static methods can be called using the scope resolution operator (::) followed by the class name, even without creating an instance of the class.

5. What is the difference between invoking a method using an object and a pointer to an object in C++?

Invoking a method using an object directly will call the method for that specific object, while invoking a method using a pointer to an object will call the method for the object pointed to by the pointer. Additionally, invoking a method using a pointer allows you to access and modify the object's data members, while invoking a method using an object does not.

Suggested for: C++ How to invoke an object's method in a different file

Replies
1
Views
240
Replies
14
Views
3K
Replies
9
Views
2K
Replies
2
Views
905
Replies
33
Views
1K
Replies
10
Views
1K
Replies
5
Views
1K
Back
Top