tags:

views:

48

answers:

5

Hi,

Warning: This is most likely a very silly question.

I have an object that resides in its own header file and is created in main, I would like to update the said object using a function that is declared in a separate file. If I'm not clear please let me know and I will try and explain my self better.

//main
#include"Object.h"
int main(){
    Object obj;  
}

Assume that the above is the main, and obj is created.

//functions file which is separate from both the main and object.h
void updateObj(int someNum){
    //how can I do this??
    obj.callingObject(someNum);
}

Thank You

A: 

Simple answer , as the code is written - you can't. You would need to make the object global:

#include"Object.h"

Object obj;  

void updateObj(int someNum){
    extern Object obj;
    obj.callingObject(someNum);
}


int main(){
    updateObj(42);
}

But you really, really don't want to do this.

anon
+2  A: 

The Object you declare in main is in the scope of main. Therefore "updateObj" can't have access to it. One of the following would make this work:

// This (IMO) is a wrong way. Unless perhaps obj is a singleton and then you should just make it such.
//main
Object obj;
int main()
{
}

// functions file
extern Object obj;
void updateObj(int someNum)
{
   obj.callingObject(somenum)
}

Another way would be to write:

void updateObj(int somnum, Object& obj)
{
}
Craig W. Wright
Second code option is better, IMO.
FrustratedWithFormsDesigner
Yes, agreed. Unless Object is a singleton, but then you'd want to go through the trouble of actually implementing it as such, perhaps using Loki's singleton implementation.
Craig W. Wright
A: 

You could make obj a global:

// Object.h
//...
extern Object g_obj;

and:

// functions file
void updateObj(int someNum){
    g_obj.callingObject(someNum);
}

But an approach that would be more preferable is to pass a reference to an Object to updateObj:

// functions file
void updateObj(Object& obj, int someNum){
    obj.callingObject(someNum);
}
Daniel Trebbien
A: 

This is bad style because it assumes the user of your header file knows that there has to be a magical object called obj that gets changed without him knowing.

I think what you are attempting is a sort of "singleton" design pattern. It is quite common, so google should bring up plenty of good results.

Another way to do this would be to instantiate the object in Object.h, that way both the person including it and your header file can see it.

If you insist, you could include your main.cpp in your header file, just be sure to be careful about protecting your headers.

orangeoctopus
A: 

obj is visible only in the scope of main, so this won't work. One way to fix: rewrite the function to take as a parameter a reference to the object you want to modify:

void updateObj(Object& obj, int someNum) {
    obj.callingObject(someNum);
}

and then pass obj to it from main. You'll have to thread obj through any intermediate functions as well.

Another way: make obj a global variable. If obj must be constructed from main, make obj an Object* and initialize it from main:

int main() {
    obj = new Object;
}
Owen S.
Assuming that the function is called in a separate file not main how would I be able to use the object, beside using a global object? I would presume that it would be better for me to redesign most of my classes rather than using globals.
kenshee
I subscribe to the opinion that it's OK to just make global things global, but there had better be a good reason for them to be! Singletons are sometimes used instead of globals but they're effectively the same thing. Otherwise, you need to pass obj around to the functions/objects that need it. A pattern I've seen is to have a "context" object passed around that contains all the variables that might otherwise be global, so that 1) you don't have to thread them all separately and 2) you can pass a new context when calling someone without affecting you or your callers.
Owen S.