Topic: operator () ->when input or output?


Author: "Luis Bras" <nop52053@mail.telepac.pt>
Date: 1998/04/02
Raw View
I have a big problem...

I want my operator () to give a different result for input or output.
I have created a test class:

#include<iostream.h>
class CTest
{
protected:
 double a;
 double b;
public:
 // default constructor
 CTest()
 { cout<<"Default Constructor"<<endl;
  a=0.0;
  b=0.0;
 };
 // copy constructor
 CTest(const CTest& argm)
 {
  cout<<"Copy Constructor"<<endl;
  a=argm.a;
  b=1.0;
 };
 // initializer constructor
 CTest(const double argm)
 {
  cout<<"Initializer Constructor"<<endl;
  a=argm;
  b=2.0;
 };
 // destructor
 ~CTest()
 {
  cout<<"Destructor"<<endl;
 };
 // assigment operator
 CTest& operator=(const CTest& argm)
 {
  cout<<"assigment operator"<<endl;
  if (this == &argm)
   return *this;
  a=argm.a;
  b=3.0;
  return *this;
 };
 double& operator()(const int argm)
 {
  cout<<"operator()"<<endl;
  return a;
 };
 double operator()(int argm) const
 {
  cout<<"operator() const"<<endl;
  return b;
 };
};
int main()
{
 CTest t1;
 CTest t2(1.0);
 CTest t3(t2);
 CTest t4;
 double f;
 t4=t2;
 t4(1)=10;
 f=t4(1);
 cout << t4(1) << endl;
 return 0;
}

This is the result:

Default Constructor
Initializer Constructor
Copy Constructor
Default Constructor
operator()
operator()
operator()
10
Destructor
Destructor
Destructor
Destructor

Why is the operator() const not called?

Thanks in advance for any answer...

------------------------
Luis Bras
nop52053@mail.telepac.pt
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: jkanze@otelo.ibmmail.com
Date: 1998/04/04
Raw View
In article <01bd5e7b$36ef9060$9caf41c2@bras>,
  "Luis Bras" <nop52053@mail.telepac.pt> wrote:
>
> I want my operator () to give a different result for input or output.
> I have created a test class:

    [Example deleted...]
> Why is the operator() const not called?

Because there aren't any const objects.

This should really be a FAQ; the same question came up in
comp.lang.c++.moderated not more than a couple of weeks ago.
Search comp.lang.c++.moderated AND Proxy in DejaNews.  Better
yet, get Scott Meyer's "More Effective C++"; he explains this
in detail.

--
James Kanze    +33 (0)1 39 23 84 71    mailto: kanze@gabi-soft.fr
        +49 (0)69 66 45 33 10    mailto: jkanze@otelo.ibmmail.com
GABI Software, 22 rue Jacques-Lemercier, 78000 Versailles, France
Conseils en informatique orient   e objet --
              -- Beratung in objektorientierter Datenverarbeitung

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/   Now offering spam-free web-based newsreading
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Srinivas Vobilisetti <srinivas-v@usa.net>
Date: 1998/04/04
Raw View
Luis Bras wrote:
>
> int main()
> {
>  CTest t1;
>  CTest t2(1.0);
>  CTest t3(t2);
>  CTest t4;
>  double f;
>  t4=t2;
>  t4(1)=10;
>  f=t4(1);
>  cout << t4(1) << endl;
>  return 0;
> }
>
> This is the result:
>
> Default Constructor
> Initializer Constructor
> Copy Constructor
> Default Constructor
> operator()
> operator()
> operator()
> 10
> Destructor
> Destructor
> Destructor
> Destructor
>
> Why is the operator() const not called?
>
> Thanks in advance for any answer...
>

Your const operator will be called for const objects/parameters. The
non-const version will be called for non-const object whether it is used
as r-value or l-value.

Add the lines

const CTest t5(10.0);
f = t5();

Srinivas
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]