"pass object around"?

hello everybody. i have a question that i believe it's very simple.

id' like to know how to share a object between many functions.
for example, let's considere these codes (i haven't compiled this):

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int Function1();

int main(){
    Class1{
    public:
        int x;
    };

    Class1 object1;
    object1.x = 10;

    Function1();

    return 0;
}


and another file, let's say other.cpp:
other.cpp
1
2
3
4
5
int Function1(){
    int y;
    y = object1.x + 10;
    return y;
}


my question is, how can Function1 access the int object1.x? how to make this simpe code work?
it was said to me that i should simply 'pass my object around'. but what that means and how do i do that? is there anything to do with pointers?

thaaaanks in advance! :)
Pass it as a parameter. Go look at the tutorials again if you don't know how to pass parameters to functions.
and if i need to pass, instead of a int value, a whole new function (i believe it called a 'method', since it's inside a class?)

what i want is to make Function1 be able to change object1's attributes.

if i pass by reference, do i have to make a new function for every attribute i want to change?
If you want to pass a whole function you initialize it in the class1 space.
eg:

1
2
3
4
5
6
7
8
9
10
11
12
 //class1.h header file 
#ifndef class1_h;
#define class1_h; 
Class1
{
  public: 
  //methods
  int Function1();
  //data members
  int x; 
};
#endif 


then to to implement it, you go:

1
2
3
4
5
6
7
//class1.cpp impelmentation file
#include "class1.h"

int Class1::Function1(){
    x += 10; //because x is a data member, it can be access directly. 
    return x;
}


1
2
3
4
5
6
7
8
9
10
11
 //file containing main
#include "class1.h"

int main ()
{
  Class1 object1; 
  object1.x = 10; 
  object1.Function1(); 

  return 0;
}


It makes sense to keep .h and cpp separated.
Also, it is usually bad style to keep data members public. In most scenarios you make them private so they can not be changed directly. In this particular case though, it won't matter to much
I recommend you take another look at the tutorial :) but I hope this gave you a bit of insight.
Last edited on
sure, a lot if insight :P

can i just make it a bit more complicated?

1
2
3
4
5
6
7
8
9
10
11
12
//class1.h header file 
#ifndef class1_h;
#define class1_h; 
class Class1
{
  public: 
  //methods
  void Function1();
  //data members
  int x; 
};
#endif  


1
2
3
4
5
6
//class1.cpp implementation file
#include "class1.h"

void Class1::Function1(){
    std::cout << "hello!\n";
}


1
2
3
4
5
6
7
8
9
10
//file containing main
#include "class1.h"

int main ()
{
  Class1 object1; 
  object1.Function1(); 
  Function2();
  return 0;
}


1
2
3
4
//function2.cpp
Function2(){
  object1.Function1();
}


in the program i'm trying to make (a little game in SDL), i didn't want to put the whole code from Function2() inside the object1. because Function2() is a function that handles the title screen. and object1 just handles the game engine, so it has some methods and attributes that need to be accessed from many other functions...

if there's no other way, should i make a call of all needed functions from inside the object1? (that would be the game engine)
isn't there a way to make a pointer to object1, from Function2()?

thanks agains =)
sorry for the double post.
a friend of mine suggested to pass the whole class as a parameter, like this:
1
2
Function2(Class1* object1);
  object1->Function1();


i didn't knew i could do it like this. works like a charm. just have to set all the pointers (which are not set in this code. i still need to learn a bit more about pointer, as it's confusing...)

anyway, thanks for the help! :)
I'm not sure I understand the question but,

As long as you say "public:", any method and data member can be accessed simply by using "."

thus: object1.function1(); object1.mX = 50; etc
so if you need to call function in main.cpp, just place them under public

However, when you use "private:" you essentially say, only methods of this class can access this data member.

This has the advantage that you can place checks along the way.

eg:
object1.mX = 50; will not work, because the data member is private.

However you can get around this.
As I said before, data members are still visible to methods of that class.

thus you can write :

1
2
3
4
5
6
7
8
9
10
11
void class1::function1(int x)
{
  if (x > 0 && x < 100)
   {
      mX = x; 
   }
  else 
  { 
     std::cout << "... Error, out of bounds ..."
  }
}


As for the pointers, yes you can. You can make pointers of objects like any other pointer.

However, there is a syntax difference:

className* objectName = new object1;
but then to using the indirection operator, you get this clumpsy syntax:
(*object1).function1();

therefor the membership operator is created which is the same as the above syntax:

object1->function1();

for deletion simply do "delete object1".

This will call the destructor.


EDIT: I should probably mention too that classes are by default private, while struct is by default public.

Double Edit: Only now saw your answer, sorry :) damn refresh on the browser :p.
Well indeed that you can do as well.
Last edited on
Topic archived. No new replies allowed.