operator+= question

Discussion in 'C++' started by lothar.behrens@lollisoft.de, May 14, 2005.

  1. Guest

    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
    , May 14, 2005
    #1
    1. Advertising

  2. * :
    >
    > 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?
    Alf P. Steinbach, May 14, 2005
    #2
    1. Advertising

  3. Guest

    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 :)
    , May 14, 2005
    #3
  4. Rolf Magnus Guest

    Alf P. Steinbach wrote:

    > * :
    >>
    >> 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.
    Rolf Magnus, May 15, 2005
    #4
  5. Guest

    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
    , May 15, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jakob Bieling

    Q: operator void* or operator bool?

    Jakob Bieling, Mar 5, 2004, in forum: C++
    Replies:
    2
    Views:
    549
    Rob Williscroft
    Mar 5, 2004
  2. John Smith
    Replies:
    2
    Views:
    407
    Ivan Vecerina
    Oct 6, 2004
  3. Alex Vinokur
    Replies:
    4
    Views:
    3,028
    Peter Koch Larsen
    Nov 26, 2004
  4. Alex Vinokur
    Replies:
    3
    Views:
    4,991
    Jeff Schwab
    Mar 20, 2005
  5. Tim Clacy
    Replies:
    15
    Views:
    2,653
    Kanenas
    May 30, 2005
Loading...

Share This Page