public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/32944]  New: operator= not ambiguous overload but does not compile
@ 2007-07-31 12:19 Dallas at ekkySoftware dot com
  2007-07-31 12:37 ` [Bug c++/32944] " pcarlini at suse dot de
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Dallas at ekkySoftware dot com @ 2007-07-31 12:19 UTC (permalink / raw)
  To: gcc-bugs

#include <string.h>
#include <stdio.h>

class CString{
public:
        CString(){value = NULL;}
        CString(char *initValue){
                if(initValue != NULL) value = NULL; 
                else strcpy((value = new char[strlen(initValue)+1]),initValue);
        }
        ~CString(){if(value != NULL) delete [] value;}
        CString& operator=(char* newValue){
                if(value != NULL) delete [] value;
                if(newValue != NULL) value = NULL; 
                else strcpy((value = new char[strlen(newValue)+1]),newValue);
        }
        CString& operator=(CString& newValue){
                if(value != NULL) delete [] value;
                if(newValue.value != NULL) value = NULL; 
                else strcpy((value = new
char[strlen(newValue.value)+1]),newValue.value);
        }
        CString operator+(char *addValue){
                CString tempValue;
                strcpy((tempValue.value = new char[strlen(value) +
strlen(addValue) + 1]),value);
                strcat(tempValue.value,addValue);
                return CString(tempValue.value);
        }
        CString operator+(CString& addValue){
                CString tempValue;
                strcpy((tempValue.value = new char[strlen(value) +
strlen(addValue.value) + 1]),value);
                strcat(tempValue.value,addValue.value);
                return CString(tempValue.value);
        }
        char *value;
};

int main(int argc, char *argv[])
{
        CString a("First String"), b("Second String"), c;
        c = a + " - " + b;
        printf(c.value);
}


-- 
           Summary: operator= not ambiguous overload but does not compile
           Product: gcc
           Version: 4.0.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: Dallas at ekkySoftware dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32944


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c++/32944] operator= not ambiguous overload but does not compile
  2007-07-31 12:19 [Bug c++/32944] New: operator= not ambiguous overload but does not compile Dallas at ekkySoftware dot com
@ 2007-07-31 12:37 ` pcarlini at suse dot de
  2007-08-01  0:14 ` Dallas at ekkySoftware dot com
  2007-08-01  0:24 ` pinskia at gcc dot gnu dot org
  2 siblings, 0 replies; 4+ messages in thread
From: pcarlini at suse dot de @ 2007-07-31 12:37 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from pcarlini at suse dot de  2007-07-31 12:37 -------
The compiler is right: operator+ returns a *temporary* CString and you have an
assignment operator taking a *non const* reference. Just change the latter to
take a const reference.


-- 

pcarlini at suse dot de changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID
            Summary|operator= not ambiguous     |operator= not ambiguous
                   |overload but does not       |overload but does not
                   |compile                     |compile


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32944


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c++/32944] operator= not ambiguous overload but does not compile
  2007-07-31 12:19 [Bug c++/32944] New: operator= not ambiguous overload but does not compile Dallas at ekkySoftware dot com
  2007-07-31 12:37 ` [Bug c++/32944] " pcarlini at suse dot de
@ 2007-08-01  0:14 ` Dallas at ekkySoftware dot com
  2007-08-01  0:24 ` pinskia at gcc dot gnu dot org
  2 siblings, 0 replies; 4+ messages in thread
From: Dallas at ekkySoftware dot com @ 2007-08-01  0:14 UTC (permalink / raw)
  To: gcc-bugs

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 805 bytes --]



------- Comment #2 from Dallas at ekkySoftware dot com  2007-08-01 00:14 -------
The reason I was passing the reference and not the value is because is some
circumstances I can change the value of the parameter. Using the const keyword
prevents me from calling methods that can change the value – even if in this
example they do not.

Thanks,
Dallas Clarke

BSc (CS) LLB UNSW
Architect / Lead Developer 
Ekky Software


Website: http://www.ekkySoftware.com 


-- 

Dallas at ekkySoftware dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |UNCONFIRMED
         Resolution|INVALID                     |


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32944


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c++/32944] operator= not ambiguous overload but does not compile
  2007-07-31 12:19 [Bug c++/32944] New: operator= not ambiguous overload but does not compile Dallas at ekkySoftware dot com
  2007-07-31 12:37 ` [Bug c++/32944] " pcarlini at suse dot de
  2007-08-01  0:14 ` Dallas at ekkySoftware dot com
@ 2007-08-01  0:24 ` pinskia at gcc dot gnu dot org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2007-08-01  0:24 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from pinskia at gcc dot gnu dot org  2007-08-01 00:24 -------
You need to change the operator= to:
        CString& operator=(const CString& newValue){

Otherwise you cannot bind a rvalue to the reference as it will only bind to a
lvalue and returned from operator+ is a rvalue.

In C++0x there are rvalue references which you can use but that feature is not
in a released version of GCC yet.  (it is on the trunk though).


-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32944


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2007-08-01  0:24 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-07-31 12:19 [Bug c++/32944] New: operator= not ambiguous overload but does not compile Dallas at ekkySoftware dot com
2007-07-31 12:37 ` [Bug c++/32944] " pcarlini at suse dot de
2007-08-01  0:14 ` Dallas at ekkySoftware dot com
2007-08-01  0:24 ` pinskia at gcc dot gnu dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).