PDA

View Full Version : how to get the value of class variable in a non member function?




chidambaram
Apr 9, 2008, 05:25 AM
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
Apr 9, 2008, 05:33 AM
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
Apr 9, 2008, 05:43 AM
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
Apr 9, 2008, 05:47 AM
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
Apr 9, 2008, 06:07 AM
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
Apr 9, 2008, 07:15 AM
Can't you just add a accessor? I would also watch those type definitions.


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

chidambaram
Apr 9, 2008, 09:12 AM
thanks for all your reply

its working

gnasher729
Apr 9, 2008, 09:57 AM
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.

chidambaram
Apr 10, 2008, 11:13 PM
Thanks for ur advice

i will follow this..