Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

chidambaram

macrumors member
Original poster
Mar 9, 2008
67
0
hi all,

I am working in a carbon c++ application using XCode

I have a class named MainWindow

In that class i have a variable temporaryvariable.

In a member function i assigned a value to that variable

In the non member function i have to get the value of that variable

how can i get that value.

( In my class member function i call another function and it return a value. I want to use that return value in my non member function. how can i get that return value in non member function)

Thanks in advance and urgent plz...
 

cmaier

Suspended
Jul 25, 2007
25,405
33,474
California
hi all,

I am working in a carbon c++ application using XCode

I have a class named MainWindow

In that class i have a variable temporaryvariable.

In a member function i assigned a value to that variable

In the non member function i have to get the value of that variable

how can i get that value.

( In my class member function i call another function and it return a value. I want to use that return value in my non member function. how can i get that return value in non member function)

Thanks in advance and urgent plz...

if you need to use the value outside the class, it's probably not really meant to be a "temporaryvariable." Why not make it a class variable and create an accessor for non-class functions?

Alternatively, you could make it a global, but that's always a pretty horrible thing to do.

Or, you say in the class member you call another function and it returns the value you use in the class. (For clarity, let's say the function is "value=generateTempValue()".) You could make a new class:

class _tempGenerator(...)
static tempvalue;
static generateTempValue() {if already_generated, do nothing}
static getTempValue() ;
} (ignore the syntax - the point is to make the stuff class static)

and then do, from your own class:
_tempGenerator::generateTempValue();
value=_tempGenerator::getTempValue();

and of course, both functions can be combined. THis just replaces a global with a scoped variable.


and make generateTempValu()
 

chidambaram

macrumors member
Original poster
Mar 9, 2008
67
0
Thanks for ur reply...

Sorry i named a variable in class as temporaryvariable.

Actually that variable is a class variable only.

How can i use that?

The function that returns value is a function defined in another class.

So i cant able to create new class for static functions
 

cmaier

Suspended
Jul 25, 2007
25,405
33,474
California
Thanks for ur reply...

Sorry i named a variable in class as temporaryvariable.

Actually that variable is a class variable only.

How can i use that

I'm afraid now you've confused me as to what you're trying to do. Can you post a few lines of pseudocode so we can see the relationship between the various classes and variables? I'm also not sure what you mean when you say you "named a variable in class as temporaryvariable." In C, you can name volatile variables, but I'm sure that's not what you mean, and there is no other way to name a variable as "temporary."
 

chidambaram

macrumors member
Original poster
Mar 9, 2008
67
0
This is my code

MainWindow and TWindow are class





class MainWindow : public TWindow
{
int sendmessageflag;
public:
OpaqueWindowPtr* requiredvalue;
MainWindow(void);
MainWindow(int);
void checkingpointer(void);
void checktemporaryvalue(void);

}

void MainWindow::checkingpointer()
{
OpaqueWindowPtr* re1 = GetWindowRef(); // this GetWindowRef() is defined In TWindow class

requiredvalue = re1;
printf("\n Requiredvalue = %d",requiredvalue); // I get the value here
printf("\n re1 = %d",re1);
}

void MainWindow::checktemporaryvalue()
{
printf("\n \n Required value = %d",requiredvalue); // I get the value here
}

void createMainWindow (void)
{

printf(" I am main");
int mainvalue;
MainWindow* mainWindow = new MainWindow(2);
}

void ReceiveCreateSocket(void) // Non - member function
{
MainWindow* mw = new MainWindow(1);
//MainWindow mw(1);
printf("\n REquired Value in outer function = %d",mw->requiredvalue);
// here i get the value as 0 because i assigned value for the object mainWindow here i am trying to get value of mw object. so only it returns 0. how can i get the return value of function GetwindowRef() here.
if i am confusing u plz reply

}
 

pilotError

macrumors 68020
Apr 12, 2006
2,237
4
Long Island
Can't you just add a accessor? I would also watch those type definitions.

Code:
class MainWindow : public TWindow
{
int sendmessageflag;
public:
OpaqueWindowPtr* requiredvalue;
MainWindow(void);
MainWindow(int);
void checkingpointer(void);
void checktemporaryvalue(void);
OpaqueWindowPtr* getTemporaryValue();
}

OpaqueWindowPtr*  MainWindow::getTemporaryValue()
{
    return requiredvalue;
}

void ReceiveCreateSocket(void) // Non - member function
{
  MainWindow* mw = new MainWindow(1);

  printf("\n REquired Value in outer function = %d",mw.getTemporaryValue());
}
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,566
hi all,

I am working in a carbon c++ application using XCode

I have a class named MainWindow

In that class i have a variable temporaryvariable.

In a member function i assigned a value to that variable

In the non member function i have to get the value of that variable

how can i get that value.

( In my class member function i call another function and it return a value. I want to use that return value in my non member function. how can i get that return value in non member function)

Thanks in advance and urgent plz...

First make up your mind whether this is a class variable ("static" in the class definition) or a member variable. Does the variable exist only once per class, or once per object?

If it is indeed a class variable, then it is quite likely that you should only change it in a static member function (that only exists once per class). Changing it in an object is not a good idea, unless you have a very good reason to do so. You would usually also have a static member function that returns the value of that class variable; it is most likely not a good idea to give non-member functions access.

If you need to access it, then the syntax would be myclass::myvariable. Won't work unless it has been made public, and you'd have to have a very good reason to make it public.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.