Results 1 to 2 of 2

Thread: Access an object from another file.

  1. #1
    Join Date
    Aug 2008
    Posts
    52
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default 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
    Qt Code:
    1. #include <iostream>
    2. #include "alpha.h"
    3. #include "bita.h"
    4. using namespace std;
    5.  
    6. int main()
    7. {
    8. A ob(4);
    9. B bita;//<this creates a new project which is irrelevant of the one i need to pass from alpha.cpp
    10. // i need to remove that
    11. cout << ob.get_a();
    12. cout << bita.dio;//<-- This currently outputs 2 (and not 6 as i want)
    13. //cout << par;
    14. return 0;
    15. }
    To copy to clipboard, switch view to plain text mode 


    alpha.cpp
    Qt Code:
    1. #include "alpha.h"
    2. #include "bita.h"
    3. A::A(int x)
    4. {
    5. B bita;
    6. a = bita.ena;
    7. bita.dio = 6;//<---------- i want this to be accesible by ena.cpp directly
    8. }
    9.  
    10. int A::get_a()
    11. {
    12. return a;
    13. }
    To copy to clipboard, switch view to plain text mode 


    alpha.h
    Qt Code:
    1. #include "bita.h"
    2. #ifndef ALPHA_H
    3. #define ALPHA_H
    4.  
    5. class A {
    6. int a;
    7. public:
    8. A(int x);
    9. int get_a();
    10. };
    11.  
    12. #endif /*ALPHA_H_*/
    To copy to clipboard, switch view to plain text mode 


    bita.h
    Qt Code:
    1. #include "bita.h"
    2. #ifndef ALPHA_H
    3. #define ALPHA_H
    4.  
    5. class A {
    6. int a;
    7. public:
    8. A(int x);
    9. int get_a();
    10. };
    11.  
    12. #endif /*ALPHA_H_*/
    To copy to clipboard, switch view to plain text mode 


    bita.cpp
    Qt Code:
    1. #include "bita.h"
    2.  
    3. B::B(){
    4. ena = 1;
    5. dio = 2;
    6. tria = 3;
    7. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Aug 2006
    Location
    Switzerland
    Posts
    52
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Access an object from another file.

    Quote Originally Posted by cbarmpar View Post
    How can i access the object from alpha.cpp (bita.dio=6) from the main (ena.cpp).

    alpha.cpp
    Qt Code:
    1. #include "alpha.h"
    2. #include "bita.h"
    3. A::A(int x)
    4. {
    5. B bita;
    6. a = bita.ena;
    7. bita.dio = 6;//<---------- i want this to be accesible by ena.cpp directly
    8. }
    To copy to clipboard, switch view to plain text mode 
    You cannot because 'bita' is created locally in 'A' constructor. When 'A' constructor finishes it's job then 'bita' is destroyed. You can add your own destructor to 'B' which will print something and see yourself.

    You have to made 'bita' a member of 'A' class:
    Qt Code:
    1. class A {
    2. B bita;
    3. int a;
    4. public:
    5. A(int x);
    6. int get_a();
    7. B * get_bita();
    8. };
    9.  
    10. A::A(int x) {
    11. bita.dio = 6;
    12. }
    13.  
    14. B * A::get_bita() {
    15. return &bita;
    16. }
    17.  
    18. int main() {
    19. A ob(4);
    20. cout << ob.get_bita()->dio; // will print 6
    21. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by danadam; 6th September 2008 at 22:28.
    The Wheel weaves as the Wheel wills.

  3. The following user says thank you to danadam for this useful post:

    cbarmpar (6th September 2008)

Similar Threads

  1. how to include object files in .pro file
    By babu198649 in forum Newbie
    Replies: 1
    Last Post: 1st July 2008, 08:23
  2. Set up the Qt4.3.2 with Visual Studio 2005
    By lamoda in forum Installation and Deployment
    Replies: 6
    Last Post: 30th January 2008, 06:51
  3. How to create a access file .mdb
    By aiikcmo in forum Qt Programming
    Replies: 1
    Last Post: 22nd November 2007, 10:06
  4. Replies: 4
    Last Post: 26th June 2007, 19:19
  5. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 15:21

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.