how to get the value of class variable in a non member function?

Discussion in 'Mac Programming' started by chidambaram, Apr 9, 2008.

  1. macrumors member

    Joined:
    Mar 9, 2008
    #1
    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...
     
  2. macrumors G3

    Joined:
    Jul 25, 2007
    Location:
    California
    #2
    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()
     
  3. thread starter macrumors member

    Joined:
    Mar 9, 2008
    #3
    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
     
  4. macrumors G3

    Joined:
    Jul 25, 2007
    Location:
    California
    #4
    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."
     
  5. thread starter macrumors member

    Joined:
    Mar 9, 2008
    #5
    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

    }
     
  6. macrumors 68020

    pilotError

    Joined:
    Apr 12, 2006
    Location:
    Long Island
    #6
    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());
    }
    
     
  7. thread starter macrumors member

    Joined:
    Mar 9, 2008
    #7
    thanks for all your reply

    its working
     
  8. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #8
    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.
     
  9. thread starter macrumors member

    Joined:
    Mar 9, 2008
    #9
    Thanks for ur advice

    i will follow this..
     

Share This Page