Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   How to get store a value returned by a const member function? (http://www.go4expert.com/forums/store-value-returned-const-function-t8678/)

rag84dec 6Feb2008 13:14

How to get store a value returned by a const member function?
 
Hi ,
I have a const member function which returns a protected member of a class.Like this
Code:

class A
{
protected:
int Number;
public:
 int  Get_Number  (void) const  { return(Number);  }
};


I want to use the value returned by the Get_Number function.
How can i do

int temp=Data.Get_Number(); is giving me an execption(Data is of type "A&")


Can anyone help me??...

rvirma 6Feb2008 14:52

Re: How to get store a value returned by a const member function?
 
Data is object of class A . Declare it

const A Data;

asadullah.ansari 7Feb2008 13:45

Re: How to get store a value returned by a const member function?
 
Code: cpp

class MyTest
{
protected:
  int n;
public:
  int GetVal()const
  {
    return n;
  }
  void SetVal(int num)
  {
    n=num;
  }
};

int main()
{
 int temp;
 MyTest obj;
 obj.SetVal(10);
 temp=obj.GetVal();
cout<<temp;
 return 0;
}


This code has no problems. What's your problem??? Let me know exactly what u wanna?


All times are GMT +5.5. The time now is 19:48.