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
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. global access to objects / instances

global access to objects / instances

Scheduled Pinned Locked Moved Managed C++/CLI
helpquestionc++
12 Posts 2 Posters 0 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.
  • K KlaasVersteeg

    I'm quite new to programming C++ and I ran into a problem. I'm working on a program in which some objects are being used by different functions, in separate files. I created the objects in a header file and can access them from a source file. However, I would like to have an instance of the object available for access from all of my functions. How can I create such objects / instances? Any help is highly appreciated.

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #2

    I'm not sure I fully understand your question, but the usual way of doing this is to pass a reference to the object to each function that requires it. Making objects globally visible is generally less safe. For example:

    int main()
    {
    CType* object = new CType();
    // do other stuff

    // call a function, passng it reference to the object
    BOOL result = MyFunc(object);
    // etc
    

    }
    BOOL MyFunc(CType* anObject)
    {
    // do other work
    // call next function passing the object pointer
    int rc = OtherStuff(anObject);

    // etc
    

    }

    txtspeak is the realm of 9 year old children, not developers. Christian Graus

    K 1 Reply Last reply
    0
    • L Lost User

      I'm not sure I fully understand your question, but the usual way of doing this is to pass a reference to the object to each function that requires it. Making objects globally visible is generally less safe. For example:

      int main()
      {
      CType* object = new CType();
      // do other stuff

      // call a function, passng it reference to the object
      BOOL result = MyFunc(object);
      // etc
      

      }
      BOOL MyFunc(CType* anObject)
      {
      // do other work
      // call next function passing the object pointer
      int rc = OtherStuff(anObject);

      // etc
      

      }

      txtspeak is the realm of 9 year old children, not developers. Christian Graus

      K Offline
      K Offline
      KlaasVersteeg
      wrote on last edited by
      #3

      Thanks for the quick answer Richard! I tried to do this, but the problem I ran into is the following: I have a sourcefile "program.cpp" with header "program.h" in which my classes are defined. I want to pass an object from "program.cpp" to "function1.cpp", by: double^ function1result = function1(CType^ object) In function1 I should now write the function as: double^ function1(CType^ object){} When I do that, I get an "undeclared identifyer" error. When I include the "program.h" header where the class is defined I get a "type redefinition" error. So my question is how to pass these objects in a way that such errors are avoided.

      L 1 Reply Last reply
      0
      • K KlaasVersteeg

        Thanks for the quick answer Richard! I tried to do this, but the problem I ran into is the following: I have a sourcefile "program.cpp" with header "program.h" in which my classes are defined. I want to pass an object from "program.cpp" to "function1.cpp", by: double^ function1result = function1(CType^ object) In function1 I should now write the function as: double^ function1(CType^ object){} When I do that, I get an "undeclared identifyer" error. When I include the "program.h" header where the class is defined I get a "type redefinition" error. So my question is how to pass these objects in a way that such errors are avoided.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #4

        Arjen Tjallema wrote:

        When I include the "program.h" header where the class is defined I get a "type redefinition" error.

        This means you are defining the type (presumably CType) in more than one place in your source code. Without seeing more of your header and source file it's difficult to be more explicit.

        txtspeak is the realm of 9 year old children, not developers. Christian Graus

        K 1 Reply Last reply
        0
        • L Lost User

          Arjen Tjallema wrote:

          When I include the "program.h" header where the class is defined I get a "type redefinition" error.

          This means you are defining the type (presumably CType) in more than one place in your source code. Without seeing more of your header and source file it's difficult to be more explicit.

          txtspeak is the realm of 9 year old children, not developers. Christian Graus

          K Offline
          K Offline
          KlaasVersteeg
          wrote on last edited by
          #5

          Thanks again! I'll put some bits of my code here to clarify my question. The "program.h" header contains a class clGlobals:

          public ref class clGlobals
          {
          public:
          double^ airDensity;
          double^ waterDensity;
          double^ gravitation;
          };

          In my "program.cpp" source I create an instance, put some values in it and (try to) call function1:

          clGlobals^ globals = gcnew clGlobals();
          globals->airDensity = Convert::ToDouble(1.025);
          globals->waterDensity = Convert::ToDouble(1025);
          globals->gravitation = Convert::ToDouble(9.81);

          double^ load = function1();

          The "function1.h" header contains a declaration of the function:

          double^ load(clGlobals^ global);

          And the "function1.cpp" source contains the actual function:

          double^ load(clGlobals^ global)
          {
          // function stuff...
          return load;
          }

          I hope this clarifies my question.

          L 1 Reply Last reply
          0
          • K KlaasVersteeg

            Thanks again! I'll put some bits of my code here to clarify my question. The "program.h" header contains a class clGlobals:

            public ref class clGlobals
            {
            public:
            double^ airDensity;
            double^ waterDensity;
            double^ gravitation;
            };

            In my "program.cpp" source I create an instance, put some values in it and (try to) call function1:

            clGlobals^ globals = gcnew clGlobals();
            globals->airDensity = Convert::ToDouble(1.025);
            globals->waterDensity = Convert::ToDouble(1025);
            globals->gravitation = Convert::ToDouble(9.81);

            double^ load = function1();

            The "function1.h" header contains a declaration of the function:

            double^ load(clGlobals^ global);

            And the "function1.cpp" source contains the actual function:

            double^ load(clGlobals^ global)
            {
            // function stuff...
            return load;
            }

            I hope this clarifies my question.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #6

            I don't quite get some of this syntax. What does function1() do and where is it defined? Why does function load() not return a double, or does it?

            txtspeak is the realm of 9 year old children, not developers. Christian Graus

            K 1 Reply Last reply
            0
            • L Lost User

              I don't quite get some of this syntax. What does function1() do and where is it defined? Why does function load() not return a double, or does it?

              txtspeak is the realm of 9 year old children, not developers. Christian Graus

              K Offline
              K Offline
              KlaasVersteeg
              wrote on last edited by
              #7

              O, excuse me, I made a mistake in my previous post. The "function1.h" header code should be:

              double^ function1(clGlobals^ global);

              And the "function1.cpp" source code should be:

              double^ function1(clGlobals^ global)
              {
              double^ load;
              // function stuff...
              return load;
              }

              I hope it is understandable now.

              L 1 Reply Last reply
              0
              • K KlaasVersteeg

                O, excuse me, I made a mistake in my previous post. The "function1.h" header code should be:

                double^ function1(clGlobals^ global);

                And the "function1.cpp" source code should be:

                double^ function1(clGlobals^ global)
                {
                double^ load;
                // function stuff...
                return load;
                }

                I hope it is understandable now.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #8

                Arjen Tjallema wrote:

                I hope it is understandable now.

                Yes, and after correcting this line from:

                    double^ load = function1();
                

                to:

                double^ load = function1(globals);
                

                it compiles and runs fine. If you still have a problem then it is not within this code.

                txtspeak is the realm of 9 year old children, not developers. Christian Graus

                K 1 Reply Last reply
                0
                • L Lost User

                  Arjen Tjallema wrote:

                  I hope it is understandable now.

                  Yes, and after correcting this line from:

                      double^ load = function1();
                  

                  to:

                  double^ load = function1(globals);
                  

                  it compiles and runs fine. If you still have a problem then it is not within this code.

                  txtspeak is the realm of 9 year old children, not developers. Christian Graus

                  K Offline
                  K Offline
                  KlaasVersteeg
                  wrote on last edited by
                  #9

                  That's strange, I get the following error when trying to compile: function1.h(1) : error C2065: 'clGlobals' : undeclared identifier I have put the four files in a new project to isolate it from the rest of my code, but still I get this error message. Do yo have any clue where to look for the problem?

                  L 1 Reply Last reply
                  0
                  • K KlaasVersteeg

                    That's strange, I get the following error when trying to compile: function1.h(1) : error C2065: 'clGlobals' : undeclared identifier I have put the four files in a new project to isolate it from the rest of my code, but still I get this error message. Do yo have any clue where to look for the problem?

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #10

                    The best way to resolve this is to add the following lines:

                    #include "program.h"
                    #pragma once

                    at the beginning of function.h. And add:

                    #pragma once

                    at the beginning of program.h. This should ensure that the class definition for clGlobals is found in any file that includes function.h. Also the #pragma once statements ensure that the header files are processed once only per compilation unit, even if they are found in #include statements more than once.

                    txtspeak is the realm of 9 year old children, not developers. Christian Graus

                    K 1 Reply Last reply
                    0
                    • L Lost User

                      The best way to resolve this is to add the following lines:

                      #include "program.h"
                      #pragma once

                      at the beginning of function.h. And add:

                      #pragma once

                      at the beginning of program.h. This should ensure that the class definition for clGlobals is found in any file that includes function.h. Also the #pragma once statements ensure that the header files are processed once only per compilation unit, even if they are found in #include statements more than once.

                      txtspeak is the realm of 9 year old children, not developers. Christian Graus

                      K Offline
                      K Offline
                      KlaasVersteeg
                      wrote on last edited by
                      #11

                      That indeed solves the problem. Thanks a lot for your help!

                      L 1 Reply Last reply
                      0
                      • K KlaasVersteeg

                        That indeed solves the problem. Thanks a lot for your help!

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #12

                        Arjen Tjallema wrote:

                        Thanks a lot for your help!

                        You're welcome; it's good to find the solution occasionally!

                        txtspeak is the realm of 9 year old children, not developers. Christian Graus

                        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