[Kde-finance-apps] Problem Using getter and Setter Function

Thomas Baumgart thb at net-bembel.de
Wed Jun 23 15:59:00 CEST 2010


Hi all,

on Wednesday 23 June 2010 15:41:50 Mukesh Gupta wrote:


> Hello Friends,
> I am trying to use getter and setter functions in my class but facing a
> problem. My code goes as follows;
> 
> *AlkRecord.h
> *
> ...
> ....
> Class Alkrecord
> {
> ....
> public:
> 
> Q_PROPERTY(QString source READ source WRITE setsource);
> //setter function

which is better named setSource()

> void setsource (const QString& source)
> {
>     m_source=source;
> }
> 
> //getter function
>  QString source() const

better: const QString& source() const

>  {
>          return m_source;
>  }
> 
> private:
> .....
> QString  m_source;
> .....
> };
> 
> 
> 
> *AlkRecord.cpp*
> 
> .......
> ......
> AlkRecord::AlkRecord(QString const& Src, QString const& Memo, QString
>  const& Amount)
> {
>   ........
>  *source= Src;*

no, that won't work.

   m_source = Src;

is the way to go. There's no such variable called 'source' even you define a 
property of that name.

> .......
> }
> 
> 
> The error  is as follows:
> 
> *error: invalid use of member (did you forget the ‘&’ ?)
> *This error is in the line shown in bold
> 
> Shouldn't the setter function be called when i try to assing source with
> some value.

The other way around: You call the setter method to assign a value to the 
member variable.

> Also how do i get the value of m_variable using getter function.

Assuming, you have a variable called 'r' of type AlkRecord, you do this:

  AlkRecord r;
  r.setSource(QLatin1String("xyz"), ....);  // call the setter
  QString str;
  str = r.source();                         // call the getter


> Please help.

Hope that does.

-- 

Regards

Thomas Baumgart

GPG-FP: E55E D592 F45F 116B 8429   4F99 9C59 DB40 B75D D3BA
-------------------------------------------------------------
'Only wimps use backup: _real_ men just upload their important stuff
on ftp, and let the rest of the world mirror it ;)' - Linus Torvalds
-------------------------------------------------------------
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 225 bytes
Desc: This is a digitally signed message part.
Url : http://mail.kde.org/pipermail/kde-finance-apps/attachments/20100623/67d21685/attachment.sig 


More information about the Kde-finance-apps mailing list