Thread: Question about operator overloading and hard-coded strings.

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    100

    Question Question about operator overloading and hard-coded strings.

    Hello. I'm trying to review operator overloading in C++, and I've run into a problem that involves both it and typecasting. This line of code is giving me issues:

    Code:
    Integer integer = "34";
    The message is:

    Code:
    conversion from 'const char [3]' to non-scalar type 'Integer' requested|
    At first this was coming up with the following operator overload:

    Code:
    Integer Integer::operator=(const string &str)
    {
        value = str;
        return *this;
    }
    so I tried changing it to this to no avail:

    Code:
    Integer Integer::operator=(const char *str)
    {
        value = *str;
        return *this;
    }
    What's the deal? There's almost definitely a simple fix to this, but I'm at a loss at the moment as to what it is. Thanks!

  2. #2
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    257
    The problem is that, while your code looks like assignation, it really uses copy construction. You need to provide a copy constructor that takes a string literal.

    P.S. Also, operator = is supposed to return a REFERENCE not an actual copy.


    EDIT: As Elysia pointed out, this is not quite correct. It uses the constructor, not the copy constructor!
    Last edited by antred; 06-19-2012 at 12:29 PM. Reason: Because it's WRONG!

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Integer integer = "34";
    This is an initialization, the same as
    Integer integer("34");
    Initialization utilizes the constructor (not the copy constructor).

    Integer integer;
    integer = "34";
    This is assignment. It uses the assignment operator.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    257
    Quote Originally Posted by Elysia View Post
    Initialization utilizes the constructor (not the copy constructor).

    Meh, yeah you're right. Brainfart.

    @OP: Sorry about that.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    100
    Thanks. I'm kind of surprised it wasn't a more minor, technical detail about overloading syntax or typecasting.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You're probably better off making a constructor for it instead anyway rather than doing:
    Integer integer;
    integer = "34";
    Then thanks to the copy-constructor, it will allow you to do both.
    Of course you may also want to consider whether the constructor should be marked explicit.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 03-31-2012, 10:51 PM
  2. Two Hard-coded Arrays into One 2-Dimensional Vector
    By codechick in forum C++ Programming
    Replies: 11
    Last Post: 03-24-2012, 02:41 PM
  3. Question about operator overloading
    By h3ro in forum C++ Programming
    Replies: 3
    Last Post: 04-15-2008, 01:02 PM
  4. Operator overloading question
    By Great Satchmo in forum C++ Programming
    Replies: 1
    Last Post: 09-18-2004, 07:41 PM
  5. operator overloading question
    By brianptodd in forum C++ Programming
    Replies: 2
    Last Post: 05-09-2003, 09:03 PM