Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Use object of an external class

Use object of an external class

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
6 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dodoxor
    wrote on last edited by
    #1

    Hi.I'm doing a Visual c++2005 project. I've implemented a class A in a cpp file like this: class A {...constructor,methods,and so on }; Then i've to use an object of this class in another file(my form.h file of the graphic interface) and i want that the object is visible to all methods of this class so that i can use its properties. I've put my #include a.h but where must i have to declare it to use it as "a global variable"? I can use it only if i declare it in a method but it's not visible from the other methods. If i declare it in the constructor of the class the methods don't see the object. Thanks for answers, regards.

    L M 2 Replies Last reply
    0
    • D dodoxor

      Hi.I'm doing a Visual c++2005 project. I've implemented a class A in a cpp file like this: class A {...constructor,methods,and so on }; Then i've to use an object of this class in another file(my form.h file of the graphic interface) and i want that the object is visible to all methods of this class so that i can use its properties. I've put my #include a.h but where must i have to declare it to use it as "a global variable"? I can use it only if i declare it in a method but it's not visible from the other methods. If i declare it in the constructor of the class the methods don't see the object. Thanks for answers, regards.

      L Offline
      L Offline
      Larry J Siddens
      wrote on last edited by
      #2

      If I under stand your question correctly, you have something like this...

      class MyClass
      {
      public:
      MyClass() { ... }
      ~MyClass() { ... }

      void myFunction( ... );
      int  getInt();
      void setInt( int x );
      

      private:
      int myInt;
      }

      And you have your .cpp file. Then you do something like this (different source file):

      #include "myClass.h"

      MyClass myObject;

      ... Other stuff...

      Then to have it global.

      #include "MyClass.h"

      extern MyClass myObject;

      Is this what your trying to do? Larry J. Siddens

      D 1 Reply Last reply
      0
      • D dodoxor

        Hi.I'm doing a Visual c++2005 project. I've implemented a class A in a cpp file like this: class A {...constructor,methods,and so on }; Then i've to use an object of this class in another file(my form.h file of the graphic interface) and i want that the object is visible to all methods of this class so that i can use its properties. I've put my #include a.h but where must i have to declare it to use it as "a global variable"? I can use it only if i declare it in a method but it's not visible from the other methods. If i declare it in the constructor of the class the methods don't see the object. Thanks for answers, regards.

        M Offline
        M Offline
        Mark Salsbery
        wrote on last edited by
        #3

        Do you want a global variable or a member variable accessible to the class it belongs to? A global variable you define outside of any class... A a; A member variable is defined in a class... class MyClass { protected: //public, private - whatever is appropriate A a; ... } "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

        D 1 Reply Last reply
        0
        • L Larry J Siddens

          If I under stand your question correctly, you have something like this...

          class MyClass
          {
          public:
          MyClass() { ... }
          ~MyClass() { ... }

          void myFunction( ... );
          int  getInt();
          void setInt( int x );
          

          private:
          int myInt;
          }

          And you have your .cpp file. Then you do something like this (different source file):

          #include "myClass.h"

          MyClass myObject;

          ... Other stuff...

          Then to have it global.

          #include "MyClass.h"

          extern MyClass myObject;

          Is this what your trying to do? Larry J. Siddens

          D Offline
          D Offline
          dodoxor
          wrote on last edited by
          #4

          I try to explain my problem... class MyClass { public: MyClass() { ... } ~MyClass() { ... } void myFunction( ... ); int getInt(); void setInt( int x ); private: int myInt; } in a .cpp file. Then I've have my form.h where i want to use an object of Myclass #pragma once namespace WinCam { using namespace System; public ref class Form1 : public System::Windows::Forms::Form { private: System::Windows::Forms::TabPage^ tabPage1; ... public: Form1(void) { InitializeComponent(); //////...... } protected: ~Form1() {... } private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { //here i want to use an object of the a class. ob.Myfunction(); } private: System::String^ loadtext(void){ //here i want to use the same object. int n; n=ob.getint(); } .... Where and how must i have to declare Myclass ob=new Myclass()?? thanks, regards.

          1 Reply Last reply
          0
          • M Mark Salsbery

            Do you want a global variable or a member variable accessible to the class it belongs to? A global variable you define outside of any class... A a; A member variable is defined in a class... class MyClass { protected: //public, private - whatever is appropriate A a; ... } "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

            D Offline
            D Offline
            dodoxor
            wrote on last edited by
            #5

            I try to explain my problem... class MyClass { public: MyClass() { ... } ~MyClass() { ... } void myFunction( ... ); int getInt(); void setInt( int x ); private: int myInt; } in a .cpp file. Then I've have my form.h where i want to use an object of Myclass #pragma once namespace WinCam { using namespace System; public ref class Form1 : public System::Windows::Forms::Form { private: System::Windows::Forms::TabPage^ tabPage1; ... public: Form1(void) { InitializeComponent(); //////...... } protected: ~Form1() {... } private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { //here i want to use an object of the a class. ob.Myfunction(); } private: System::String^ loadtext(void){ //here i want to use the same object. int n; n=ob.getint(); } .... Where and how must i have to declare Myclass ob=new Myclass()?? thanks, regards.

            M 1 Reply Last reply
            0
            • D dodoxor

              I try to explain my problem... class MyClass { public: MyClass() { ... } ~MyClass() { ... } void myFunction( ... ); int getInt(); void setInt( int x ); private: int myInt; } in a .cpp file. Then I've have my form.h where i want to use an object of Myclass #pragma once namespace WinCam { using namespace System; public ref class Form1 : public System::Windows::Forms::Form { private: System::Windows::Forms::TabPage^ tabPage1; ... public: Form1(void) { InitializeComponent(); //////...... } protected: ~Form1() {... } private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { //here i want to use an object of the a class. ob.Myfunction(); } private: System::String^ loadtext(void){ //here i want to use the same object. int n; n=ob.getint(); } .... Where and how must i have to declare Myclass ob=new Myclass()?? thanks, regards.

              M Offline
              M Offline
              Mark Salsbery
              wrote on last edited by
              #6

              dodoxor wrote:

              Where and how must i have to declare Myclass ob=new Myclass()??

              Here's one possible way - adding a MyClass pointer member object, allocated in the constructor, freed in the destructor...

              public ref class Form1 : public System::Windows::Forms::Form
              {
              private:
              System::Windows::Forms::TabPage^ tabPage1;
              ...
              protected:
              MyClass *pMyClassObject;

              public:
              Form1(void)
              {
              InitializeComponent();
              //////......
              pMyClassObject = new MyClass();
              }

              protected:
              ~Form1()
              {
              delete pMyClassObject;
              }

              private:
              System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e)
              {
              //here i want to use an object of the a class.
              pMyClassObject->Myfunction();
              }

              System::String^ loadtext(void)
              {
              //here i want to use the same object.
              int n;
              n=pMyClassObject->getint();
              }
              }

              Here's another possible way - adding a MyClass member object...

              public ref class Form1 : public System::Windows::Forms::Form
              {
              private:
              System::Windows::Forms::TabPage^ tabPage1;
              ...
              protected:
              MyClass MyClassObject;

              public:
              Form1(void)
              {
              InitializeComponent();
              //////......
              }

              protected:
              ~Form1()
              {
              }

              private:
              System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e)
              {
              //here i want to use an object of the a class.
              MyClassObject.Myfunction();
              }

              System::String^ loadtext(void)
              {
              //here i want to use the same object.
              int n;
              n=MyClassObject.getint();
              }
              }

              "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups