operator+= question

L

lothar.behrens

Hi,

I have the following definition of a pure abstract class:

class lb_I_String {

// ...

virtual lb_I_String* LB_STDCALL operator += (const char* toAppend) =
0;

// ...

};

And use it like this:

lb_I_String* newWhereClause = getStringInstance(); // A fictive
'instance maker' :)

newWhereClause = " where ";

newWhereClause += "column1 = ";


Why I get these error messages ?


lbDatabaseForm.cpp: In member function `void
lbDatabaseDialog::updateFromMaster()':
lbDatabaseForm.cpp:1904: error: cannot convert `const char[8]' to
`lb_I_String*
' in assignment
lbDatabaseForm.cpp:1906: error: invalid operands of types
`lb_I_String*' and `
const char[11]' to binary `operator+'
lbDatabaseForm.cpp:1906: error: in evaluation of `operator+=(class
lb_I_String*, const char[11])'

Thanks

Lothar
 
A

Alf P. Steinbach

* (e-mail address removed):
I have the following definition of a pure abstract class:

class lb_I_String {

I don't understand that name.

Presumably others won't understand it, either.

And it's an open invitation to speling erors.

// ...

virtual lb_I_String* LB_STDCALL operator += (const char* toAppend) =
0;

Should be

virtual lb_I_String& operator+=( char const toAppend[] ) = 0;


// ...

};

And use it like this:

lb_I_String* newWhereClause = getStringInstance(); // A fictive
'instance maker' :)

newWhereClause = " where ";

newWhereClause += "column1 = ";

This invokes pointer arithmetic, not a user-defined operator.
 
L

lothar.behrens

Thanks.

About your comment:

It should be an interface like the ones from M$ COM. Maybe there is a
more usual standard.

E.g. IString for interface of a string.

Ok, this would be a desision, I would make at any time. But not now :)
 
R

Rolf Magnus

Alf said:
* (e-mail address removed):
I have the following definition of a pure abstract class:

class lb_I_String {

I don't understand that name.

Presumably others won't understand it, either.

And it's an open invitation to speling erors.

// ...

virtual lb_I_String* LB_STDCALL operator += (const char* toAppend) =
0;

Should be

virtual lb_I_String& operator+=( char const toAppend[] ) = 0;

I'd not recommend to use [] for pointer parameters. If it's a pointer, just
write it down as one.
This invokes pointer arithmetic, not a user-defined operator.

To elaborate a bit: newWhereClause is a pointer, so the operator= and
operator+= are called for pointers, not for lb_I_String objects. So your
operator defined above won't be used at all.
 
L

lothar.behrens

I really do it a bit different.

Only declare char* as the parameter for the operator. Before, that was
a try.

*newWhereClause = " where ";

*newWhereClause += " column1 = ";

This is a little ugly, but I do not know a work around. My real code
uses smart pointers,
that I have defined. Also defining the operators in the smart pointers,
is not good, I think.

But at least it defines some dereference operators. Then it is this way
(UAP = unknown automatic pointer (handle body pattern I think)):

UAP(lb_I_String, colName)
colName = myMasterFormDefinition->getMasterColumn(i);

*newWhereClause += *&colName; // * = reference, *& = dereferenced
pointer-pointer
bool isChar = myMasterFormDefinition->isCharacterColumn(i);

if (isChar)
*newWhereClause += " = '";
else
*newWhereClause += " = ";

In both situations, a normal pointer or a smart pointer with an
operator* or & must be used. Could I declare these operators in my
smart pointer so that it will be 'forwarded' ?

Thanks

Lothar
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top