stanm@thumper.bellcore.com (Stan Moyer) (06/12/91)
I am having trouble using a constructor as a default argument for a
function. I am using cfront V2.1 Here is a sample of what I have tried
and the resulting error messages.
class X {
public:
X(int);
};
const int DEF_X = 0;
class Y {
public:
Y();
void f(X x=0);
void g(X x=X(0));
void h(X x=DEF_X);
};
functions f & h give me the following error message:
"sorry, not implemented: constructor needed for argument initializer"
function g gives me the following error message:
"sorry, not implemented: constructor as default argument"
I really didn't expect f to work, but I thought g & h would be valid.
Is this not allowed in C++? Can you only use fundamental data types as
default arguments? Does this not work because a default argument must be
a compile-time constant? I could not find anything about it in the ARM.
Any help would be appreciated.
Thanks in advance,
Stan Moyer
<stanm@thumper.bellcore.com>; Bellcore, MRE-2A219;
445 South Street, Morristown, NJ 07962-1910
voice: +201.829.4923; fax: +201.984.2283stanm@thumper.bellcore.com (Stan Moyer) (06/13/91)
In regards to my earlier posting, I've learned the following:
This piece of code:
#include <stream.h>
class X {
public:
X(int i) { cout << "X(int): " << i << "\n"; }
};
const int DEF_X = 0;
class Y {
public:
Y() {}
void f(X x=0) {}
void g(X x=X(0)) {}
void h(X x=DEF_X) {}
};
main ()
{
Y y;
y.f();
y.g();
y.h();
}
compiles and produces the following output with g++:
X(int): 0
X(int): 0
X(int): 0
However, when I try to compile with cfront, I get the following output:
line 13: sorry, not implemented: constructor needed for argument initializer
line 14: sorry, not implemented: constructor as default argument
line 15: sorry, not implemented: constructor needed for argument initializer
I have learned that "sorry, not implemented" does not mean that there is
an error, but rather "this is probably valid C++, but we haven't
implemented it in the compiler." Hopefully a future version (3.0?) of
cfront will implement this, but until then I'll have to work around it.
Thanks to Martin Hitz and Brian Kennedy for their responses.
Stan Moyer
<stanm@thumper.bellcore.com>; Bellcore, MRE-2A219;
445 South Street, Morristown, NJ 07962-1910
voice: +201.829.4923; fax: +201.984.2283