apologies if this answer should be obvious, perhaps this has a pattern name like "dopey assignment overload" and have not been searching by right topic - redirects most welcome code extracts:
class MySet {
private:
int lower;
int upper;
char *data;
public:
MySet( int l, int u, const char *val = 0 );
MySet &operator=( MySet &rhs );
MySet &operator=( const char *val );
};
int main(int i, const char *argv[])
{
MySet A(1,10);
A = "hello";
MySet B(1,10, "hello");
MySet C(1,10) = "hello";
}
assignments for A and B work but for C then g++ whines....: myset.cxx:19:16: error: expected ‘,’ or ‘;’ before ‘=’ token
is not a big issue but have been trying to understand why is not valid.
thanks for any helpful replies.
more info... guess am meant to edit my op?
on puzzling out why the above is considered illegal, have been looking for a definitive reason ... and of course a trick to make such notation be accepted by the compiler.
considering the similar uses that are legal
struct toons {
char first[10];
} flintstone[] = {
"fred",
"wilma",
""
};
works from couple decades of C compilers, as does:
typedef struct {
char family[10];
struct {
char name[10];
} members[6];
} toongroup;
toongroup rubble = {
"rubble",
{
"barney",
"wilma",
""
}
}
toongroup rubble = {
"rubble",
{
"barney",
"wilma",
"", "", "", ""
}
};
toongroup jetsons = { "jetson", { "george", "jane", "", "", "", "" } };
all honored by c++ compilers. can have member functions on the structure and it still works:
typedef struct toongroup {
public:
bool add( const char *newbaby ) {
for(int i; i<6; i++) {
if(strlen(members[i].name) == 0) {
strcpy(members[i].name, newbaby);
return true;
}
if(strcmp(members[i].name, newbaby) == 0)
return false;
}
return false;
}
char family[10];
struct {
char name[10];
} members[6];
} toongroup;
toongroup jetsons = { "jetson", { "george", "jane", "", "", "", "" } };
jetsons.add( "elroy" );
objections start when adding a constructor:
typedef struct toongroup {
toongroup( const char *f, const char *m, ... ) {
}
error: in C++98 ‘rubble’ must be initialized by constructor, not by ‘{...}’
error: could not convert ‘{"rubble", {"barney", "wilma", "", "", "", ""}}’ from ‘<brace-enclosed initializer list>’ to ‘toongroup‘
wondering if it could be a matter of using the right initializer so compiler can match the constructor
toongroup empty;
toongroup nobody = empty;
toongroup rubble( "rubble", "barney", "wilma", "" );
even
toongroup empty;
toongroup cancelled;
toongroup nobody = empty;
toongroup rubble( "rubble", "barney", "wilma", "" );
toongroup jetsons( "jetson", "george", "jane", "" );
jetsons.add( "elroy" );
adding
toongroup &operator=( toons &t );
allows
jetsons = cancelled;
but not
toongroup jetsons( "jetson", "george", "jane", "" ) = cancelled;
apologies for this ending up a long post, but am looking for some reference as to why the constructor + assignment is rejected and any ideas on how/if this can be accommodated.
in the notation for MySet this would be a familiar statement, before i declared it illegal wanted to check if am missing a trick.
thanks.