Results 1 to 10 of 13
Hi all,
I am new to Linux and c++ programming.
What I am trying to do is to declare an object in a .cpp and be able to access it ...
- 09-06-2008 #1Just Joined!
- Join Date
- Sep 2007
- Posts
- 33
Access an object from another file.
Hi all,
I am new to Linux and c++ programming.
What I am trying to do is to declare an object in a .cpp and be able to access it from main.At the moment my code creates a new object in the ena.cpp and cannot use the one from the alpha.cpp. How can i access the object from alpha.cpp (bita.dio=6) from the main (ena.cpp).
Many thanks in advance.
ena.cpp
Code:#include <iostream> #include "alpha.h" #include "bita.h" using namespace std; int main() { A ob(4); B bita;//<this creates a new project which is irrelevant of the one i need to pass from alpha.cpp // i need to remove that cout << ob.get_a(); cout << bita.dio;//<-- This currently outputs 2 (and not 6 as i want) //cout << par; return 0; }
alpha.cpp
Code:#include "alpha.h" #include "bita.h" A::A(int x) { B bita; a = bita.ena; bita.dio = 6;//<---------- i want this to be accesible by ena.cpp directly } int A::get_a() { return a; }
alpha.h
Code:#include "bita.h" #ifndef ALPHA_H #define ALPHA_H class A { int a; public: A(int x); int get_a(); }; #endif /*ALPHA_H_*/
bita.h
Code:#include "bita.h" #ifndef ALPHA_H #define ALPHA_H class A { int a; public: A(int x); int get_a(); }; #endif /*ALPHA_H_*/
bita.cpp
Code:#include "bita.h" B::B(){ ena = 1; dio = 2; tria = 3; }
- 09-06-2008 #2
huh??
I'm not sure what your trying to do here....is this all the code your working with?
- 09-06-2008 #3Just Joined!
- Join Date
- Sep 2007
- Posts
- 33
probably using the extern
Yes friend,
that's all the code.
I don't know how i can have access to an object created outside the ena.cpp.
Is it through the use of extern keyword how can i use the extern keyword with an object?
Regards
- 09-06-2008 #4
Oh...
You can use extern keyword but the convention is to use the #include "header.h"
I think this is what you want, if you have three programs:
main.cpp
one.cpp
two.cpp
and two header files
one.h
two.h
where main.cpp has the main function and includes one.h and two.h
#include "one.h"
#include "two.h"
int main(int argc, char**argv)
{
return 0;
}
to compile these files....
g++ -c one.cpp
g++ -c two.cpp
g++ -c main.cpp
g++ two.o one.o main.o -o main
Hope this helps
- 09-06-2008 #5Just Joined!
- Join Date
- Sep 2007
- Posts
- 33
can you provide an example code?
many thanks for the reply.
Can you please show me an example on hot to use the extern keyword for my object. I can use it for variables but nit sure at all on hot to use it for objects.
Kindest regards
- 09-06-2008 #6
to use extern for an object you defined you would have to describe it to the compiler so that it could allocate the memory need...to do that you could typedef it but then you'd have to cast it to your object so you can use the functionality but if you can cast it to the object you defined then you wouldn't need the extern keyword....do you see the cycle here....
Why are you using extern?
- 09-06-2008 #7
I got this to work...but why would you duplicate the code with extern...why not just create a proper header file and include it....because the result is the same and a header file is easier to maintain
Note: you can remove the extern "C++" and this compiles fine
Code:#include <iostream> extern "C++" class a { public: a(int size); ~a(); int getx() const; void setx(int val); private: int x; }; int main(int argc, char**argv) { a mya(12); std::cout<<mya.getx()<<"\n"; return 0; }
- 09-07-2008 #8
object file with class definition
object with class definition
Code:class a { public: a(int size); ~a(); int getx() const; void setx(int val); private: int x; }; a::a(int size) :x(size) {} a::~a() {} int a::getx() const { return x; } void a::setx(int val) { x = val; }
- 09-07-2008 #9Just Joined!
- Join Date
- Sep 2007
- Posts
- 33
many thanks
Many thanks for your time.
I think i now know what you mean. What i actually wanted is to create an object in a header file lets say header1.h implement it in header1.cpp. Make some calculations and alterations in header1.cpp and then be able to access this object from the main.cpp directly.
Behind all that is a project in qt. When i have a form i need to make an object which belongs to this form (and hence to this class) accesible by the main.
Many thnaks for your time once again
- 09-07-2008 #10are you talking about self modifying code? i.e. being able to change header1.cpp while the program is running and see the changes in the running executable?Make some calculations and alterations in header1.cpp and then be able to access this object from the main.cpp directly.
That is to say without exiting the program and re-compiling the project.
If this is what you want then good luck...you'll have to defeat the OS and few other things to manage that....
Or are you talking about two separate executables communicating via some interprocess communication IPC?


Reply With Quote