Why does this constructor-usage not compile ?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: Why does this constructor-usage not compile ?

  1. #1
    Join Date
    Apr 2004
    Posts
    20

    Why does this constructor-usage not compile ?

    Hi,

    assume a simple class like this (string-header has been included and "using namespace std" has been used):

    class someclass
    {
    private:
    string m_string;

    public:
    someclass(string& str){m_string = str;}
    };

    now I want to use this class in a programm like this

    someclass* cinst = new someclass(string("test"));

    but this doesn't compile without error. have you got any idea why ? the compiler-error occures at the line in the program where I want to instantiate "someclass".

    if I use the following everything works fine:

    string str = "test";

    someclass* cinst = new someclass(str);

    thank you

    highhead
    Last edited by highhead; April 19th, 2004 at 04:21 PM.

  2. #2
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    What error does the compiler give you? The syntax is correct and the following code compiles just fine:
    Code:
    #include <string>
    
    using namespace std;
    
    
    class someclass
    {
      private:
      string m_string;
    
      public:
      someclass(string& str){m_string = str;}
    };
    
    int main()
    {
      someclass* cinst = new someclass(string("test"));
    
      delete cinst;
      return 0;
    }
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  3. #3
    Join Date
    Mar 2004
    Location
    Israel
    Posts
    638
    someclass(string& str){m_string = str;}
    AND
    someclass* cinst = new someclass(string("test"));

    makes no sense.
    string("test") is a temporary object, therefore keeping its reference for a new object is illegal.
    **** **** **** **** **/**

  4. #4
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588
    Oh yes indeed. I saw a const in the constructor which didn't exist. Strangely enough VC6 does not complain.

    Anyways, the solution is to change the constructor to:
    Code:
    someclass(const string& str){m_string = str;}
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  5. #5
    Join Date
    Apr 2004
    Posts
    20

    TO GUISL

    Since the constructor of someclass copies the contents of the temporary variable immediately (did I mention) is copied to a internal string-instance in someclass, this is no problem.

    there's NO reference kept illegally. the operator= of std::string copies the contents of the string-reference and not the reference.

  6. #6
    Join Date
    Apr 1999
    Posts
    27,431
    The code that Yves posted does not compile using the Comeau compiler:
    Code:
    #include <string>
    
    using namespace std;
    
    class someclass
    {
      private:
      string m_string;
    
      public:
      someclass(string& str){m_string = str;}
    };
    
    int main()
    {
      someclass* cinst = new someclass(string("test"));
    
      delete cinst;
      return 0;
    }
    
    ComeauTest.c", line 17: error: no instance of constructor "someclass::someclass"
              matches the argument list
                The argument types that you used are: (std::string)
        someclass* cinst = new someclass(string("test"));
                                         ^
    
    1 error detected in the compilation of "ComeauTest.c".
    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Apr 2004
    Posts
    20

    TO IVES

    Hi Ives,

    the tanslated (from my german compiler-version) error-message reads:

    ERROR: "const someclass & cannot be initiallized with "string""

    strange, strange....

    maybe it's something with my borland-compiler.

    thanks

    highhead

  8. #8
    Join Date
    Apr 1999
    Posts
    27,431

    Re: TO GUISL

    Originally posted by highhead
    there's NO reference kept illegally. the operator= of std::string copies the contents of the string-reference and not the reference.
    Well, the Comeau compiler (which is one of the most stringent around, and ANSI compliant) begs to differ with your assertions.

    The solution is to do this:
    Code:
    someclass(const std::string& str) {
        m_string = str;
    }
    Also on a side note, you shouldn't be specifying "using namespace std" in a header file.

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    Mar 2004
    Location
    Israel
    Posts
    638
    exactly that what Yves wanted to achieve!!
    this technique is described also in Meyers's "Effevtive C++"
    in order to avoid such problems with temporary objects.
    The compiler is enforcing it now.
    **** **** **** **** **/**

  10. #10
    Join Date
    Apr 2004
    Posts
    20

    To Paul

    I don't know and use the comeau-compiler, but it seems, that it is the same problem as with the borland-compiler I use.

    strange, strange...

    thanks for your report

    highhead

  11. #11
    Join Date
    Apr 2004
    Posts
    20

    Thanks to you all

    The problem is, as I understood specifiying the parameter of the someclass-constructor as const. ok, I'll try it.

    the time is now 0:01 here, and i'm tired.

    good night and thanks again

    highhead

  12. #12
    Join Date
    Apr 1999
    Posts
    27,431

    Re: To Paul

    Originally posted by highhead
    I don't know and use the comeau-compiler, but it seems, that it is the same problem as with the borland-compiler I use.

    strange, strange...

    thanks for your report

    highhead
    I highly doubt that there is any problem.

    Regards,

    Paul McKenzie

  13. #13
    Join Date
    Aug 2002
    Location
    Madrid
    Posts
    4,588

    Re: TO GUISL

    Originally posted by highhead there's NO reference kept illegally. the operator= of std::string copies the contents of the string-reference and not the reference.
    The argument to the constructor (string &str) would take a reference of a temporary and hence it's disallowed. You don't use that reference later, but it's still wrong.

    The problem is, as I understood specifiying the parameter of the someclass-constructor as const. ok, I'll try it.
    If you specify the template parameter as a const reference, then it's OK to pass temporaries into it. So make the fix I suggested above and it will be OK.

    The problem lies in VC6. It should flag it as an error (at least a warning would be nice), but doesn't. Borland and Comeau are correct.
    Get this small utility to do basic syntax highlighting in vBulletin forums (like Codeguru) easily.
    Supports C++ and VB out of the box, but can be configured for other languages.

  14. #14
    Join Date
    Apr 2004
    Posts
    20

    Thanks Ives

    I did specify the parameter as const as you told and everything is in fact ok, very ok
    Yes, yes... Microsoft-Compilers ...I better say nothing about them...

    Thanks again

    highhead

  15. #15
    Join Date
    Mar 2002
    Location
    NY, USA
    Posts
    12,097
    Yves, your point on the const, is real close, but a little off IMHO [and reading various other sources].

    The problem is not that you could keep a reference to the non-const object (the temporary), for you can still do this with the temporary.

    The problem is that the temporary object could be MODIFIED, but there is nop external handle to the object (it is a temporary), therefore the modifications could never be accessed outside the calling scope.

    Since the usage of a non-const reference, implies that you want to change the object outside the scope of the function, the compiler stops you.

    The bottom line is that all parameters pasted should be const in all cases UNLESS there is an explicit desire to modify the object. There were even some voices in the early days (circa 1992) who proposed "const" being the default for all parameters and using a "non-const" keyword!
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009
    In theory, there is no difference between theory and paractice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center