Topic: coercion using operator, in a ctor


Author: tmaguire@us.ibm.com (Tom Maguire)
Date: 1997/09/02
Raw View
I'm trying to use an operator, overloading scheme to simplify a
classes ctor argument signature.  Basically, I need to provide
a number of arguments any one of which can be defaulted.  To
accomplish this I'm doing something similar to the following:

class args
{
   public:
 args()
 :i(5),
  f(10.1f),
  pc(0)
 {
 }
 virtual ~args();
 args& operator,(int ai);
 args& operator,(float af);
 args& operator,(const char* apc);

   private:
 int i;
 float f;
 char* pc;
};

class testargs
{
   public:
 testargs(args& aArgs);
};


int main(void)
{
 args a,b,c;

 test test1( a,11.1f );
 test test2( b,"another" );
 test test3( c,11.2f,5,"why not" );
}


The above function does not compile because the testarg
ctor argument signature is not matched.  Shouldn't the unary operator,
be applied as a conversion function to the arguments?  If I change
the operator, overloads to operator| overloads, or if I add parenthesis
around the argument to give it precedence everything compiles fine.
Shouldn't all unary operators be handled the same?

Tom Maguire
tmaguire@us.ibm.com
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]