kenshee
kenshee

Reputation: 157

Classes and updating the data

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

Upvotes: 1

Views: 188

Answers (5)

Owen S.
Owen S.

Reputation: 7855

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;
}

Upvotes: 0

Donald Miner
Donald Miner

Reputation: 39913

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.

Upvotes: 0

Daniel Trebbien
Daniel Trebbien

Reputation: 39208

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);
}

Upvotes: 0

Craig Wright
Craig Wright

Reputation: 1605

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)
{
}

Upvotes: 2

anon
anon

Reputation:

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.

Upvotes: 0

Related Questions