Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > operator+= question

Reply
Thread Tools

operator+= question

 
 
lothar.behrens@lollisoft.de
Guest
Posts: n/a
 
      05-14-2005
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

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      05-14-2005
* http://www.velocityreviews.com/forums/(E-Mail 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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
 
 
 
lothar.behrens@lollisoft.de
Guest
Posts: n/a
 
      05-14-2005
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

 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      05-15-2005
Alf P. Steinbach wrote:

> * (E-Mail 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.

>>
>> // ...
>>
>> };
>>
>> 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.


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.

 
Reply With Quote
 
lothar.behrens@lollisoft.de
Guest
Posts: n/a
 
      05-15-2005
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
question row filter (more of sql query question) =?Utf-8?B?YW5kcmV3MDA3?= ASP .Net 2 10-06-2005 01:07 PM
Quick Question - Newby Question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM
Question on Transcender Question :-) eddiec MCSE 6 05-20-2004 06:59 AM
Question re: features of the 831 router (also a 924 question) Wayne Cisco 0 03-02-2004 07:57 PM
Syntax Question - Novice Question sean ASP .Net 1 10-20-2003 12:18 PM



Advertisments