Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Modifying class data without instanciation? (An approach problem!)

Status
Not open for further replies.

Astrocloud

Technical User
May 29, 2007
13
/*My problem (of late) is that I am creating commands in C++ that will be called from sicstus prolog.

Basically I am creating a toolset that will be called from prolog. There is a sicstus compiler for this -so I won't bore you with the details.

The problem I have is that every tool will run as an independent procedure. Which means that there is no main. However, there are variables which need to be passed between functions. I've created a class and class functions to hold them.*/

class myItems {
private:
long cost;
long maxCost[100];
public:
myItems(void);

void play_with_cost();
//etc
}

void myItems::play_with_cost() {
cost =12;
maxCost[12] = 0; //or whatever
}

/*The problem is (and this is conceptual): that there is no instance of myItems when it comes to acting on:

Thus the new tool:*/

int runPlaywithCosts(int beerpong, long foo) {

myItems::play_with_cost()
//coding or more blah blah here...

return(SP_SUCCESS); //this statement is necessary for building tool

/*Can I make a global instance of a class? Should I?*/
/*Many thanks*/
/*Eric*/
 
Why can't you create an instance and then use that instance?

If you only need 1 instance, you can create a singleton.
 
The problem is solved with a global instantiation (which I am unclear of the syntax of how to reference within a function.

With help from a coworker (who is extremely busy right now) I got this far:

__________________________



//The problem is with declaring global variable pointers
//

int resetCostState() {

* GLOBAL_DATA_PTR; //does not like this declaration

&GLOBAL_DATA_PTR.reset_cost_state(); //if following declaration is made then it chokes here instead

return(SP_SUCCESS);
};

class clCost {
private:


long MaxCost;
//...
public:
clCost(void);

//create_global_data();
void reset_cost_state();//...
};


clCost::clCost(){
reset_cost_state();
}

//...


clCost * GLOBAL_DATA_PTR;//1) calling global data pointer so that we can pass data between procedures

void main () {
GLOBAL_DATA_PTR = new clCost();//2) instantiation of Global
}
 
Please use [ignore]
Code:
[/ignore]
tags when posting code.


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Alternatively make the array and the function in the class static.
 
The answer of the issue that I was having on this particular line is fixed by two things:



#1 create the global pointer to instantiation shortly after the class declaration

#2 the syntax for calling a global class is not what I was trying:

Code:
int resetCostState() {
     // the following is bad no need as instantiation is global
    // * GLOBAL_DATA_PTR; //does not like this declaration

    GLOBAL_DATA_PTR->reset_cost_state(); //now it calls.

    return(SP_SUCCESS);
};
 

Meant
Code:
int resetCostState() {
     // the following is bad no need as instantiation is global
    // * GLOBAL_DATA_PTR; //does not like this declaration

    GLOBAL_DATA_PTR->reset_cost_state(); //now it calls.

    return(SP_SUCCESS);
};
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top