CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Nov 2008
    Posts
    39

    Question object constructor scope

    I have done a few searches, however, have not come across the information I need.

    I understand basic scope rules and how scope in code generally works. What I am a bit confused with is this:

    due to seeing code similar to this, I am assuming that it won't create any sort of memory leak...
    Code:
    function( class_constructor( constructor_arg ) );
    please correct that assumption if it is incorrect.

    what I am wondering, is if i have written a class that overloads the insertion operator to take a string as the argument ( internally it inserts that string into a file stream and does a couple other things ) is this valid, or will it cause memory leaks by not destroying the string?
    Code:
    my_class  class_obj;
    class_obj << string( "initial string to create" );
    and yes, i understand that there would be no need to actually use the code above. again, I may be wrong, so please do correct me if there is a simpler way, but it seemed to me, that the easiest way to convert an integer to a string was to insert it into a stringstream, then extract a string. so i wrote a function that does this and returns a c++ string type. so, my final question is, based on the below;
    Code:
    // I generally use this to write log entries to a file, where putting 
    // an integer value between two string literals is useful
    class_obj << string( "the value of i = " + int_to_str( var1 ) + "and something else" );
    will passing the constructor as a parameter to the insertion operator, or the returned string from the function int_to_str cause memory leaks?
    if not, when do those objects actually go out of scope?

    any help will be greatly appreciated, and i do apologize if i have missed resources or information where this question has already been answered. if that is the case, a link to them would be most appreciated, thank you.

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: object constructor scope

    You can not "pass a constructor". What BOOK are you using to learn C++ in an organized and structured manner??????

    The code you posted INVOKES a constructor which cretes an instance of the class, and poasses this instance to the routine. When the instance is not longer accessible, it will authmatically have its destructor called.

    This should be in one of the very first chapeters of YOUR book......
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; 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

  3. #3
    Join Date
    Aug 2007
    Posts
    858

    Re: object constructor scope

    what I am wondering, is if i have written a class that overloads the insertion operator to take a string as the argument ( internally it inserts that string into a file stream and does a couple other things ) is this valid, or will it cause memory leaks by not destroying the string?
    If you don't use new, there's nothing to be leaked. The compiler creates a temporary object, and the compiler is responsible for cleaning it up. And FYI, the stream operators are functions just like any other:

    istream& operator >>(istream&, sometype);
    ostream& operator <<(ostream&, sometype);

    There's no difference between them and the first function you posted, you're just allowed to type them a bit differently.

    if not, when do those objects actually go out of scope?
    I believe on that particular example the string returned by int_to_str() will go out of scope when the string constructor finishes, and the string passed to your operator << will go out of scope when that function completes.

  4. #4
    Join Date
    Nov 2008
    Posts
    39

    Re: object constructor scope

    thank you, speedo, for actually answering my question. it is much appreciated.

    @cpuwiz - i'm sorry that my poor diction in asking the question has caused you to become so condescending. yes, it was not the proper word choice for the thought that I was trying to convey, a mistake on my part, but I don't think that warranted your response.

  5. #5
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: object constructor scope

    Quote Originally Posted by Mal Reynolds View Post
    @cpuwiz - i'm sorry that my poor diction in asking the question has caused you to become so condescending. yes, it was not the proper word choice for the thought that I was trying to convey, a mistake on my part, but I don't think that warranted your response.
    Programming is a very detailed and specific environment. The usage of certain words have definate meanings. Using the wrong word conveys a completely different concept.

    This is also one of the common indicators that a person is not following a careful, organized course of study of the language. If one is working froma good book, and studies the chapter on constructors, they will NEVER see the phrase (passing a constructor *); when studing the chapter on functions and parameter passing, they will not see it either.

    However when one picks up random or disorganized pieces, the will only know a litle about each, and tend to make incorrect inferences. This causes more difficulty for them and leads to bad designs,and high fustration levels. They then (typically) proceed to posting on various forums, asking questions which would never have come up, had they studied in the first place. This results in multiple people wasting their time, and also adds to the confusing (and incorrect) information that is on the web, further spreading the "disease".

    -- And that is why my initial reply was phrased in exactly the way it was.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; 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

  6. #6
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: object constructor scope

    'Pass by pointer' is a phrase that sometimes gets people upset. It's difficult to imagine it meaning anything other than an abbreviation of 'Pass a value via a pointer' but that doesn't stop some people jumping up and down and foaming at the mouth in pedantic indignation!
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  7. #7
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: object constructor scope

    Quote Originally Posted by JohnW@Wessex View Post
    'Pass by pointer' is a phrase that sometimes gets people upset. It's difficult to imagine it meaning anything other than an abbreviation of 'Pass a value via a pointer' but that doesn't stop some people jumping up and down and foaming at the mouth in pedantic indignation!
    I think there is a difference between the two situations..

    But consider this...
    Code:
    void foo(char *p) {}
    
    char data[10];
    char *ptr = data;
    foo(ptr);
    Here we are "passing a pointer by value" which happens to also be "passing the address of data by pointer"..then you can get to:
    Code:
    void foo(char **p) {}
    
    char data[10];
    char *ptr = data;
    foo(&ptr);
    Consider what would have to be said in words (and someting like "char star star ptr" does not count) so that a reader of the written text (no code) could reliabily duplicate the above code.

    (John, you already know this - it is intended for other readers) This is one of the reasons why we always ask for minimal yet complete code in the post (properly tagged and formatted, of course). Then there is no ambiguity.

    When you add in the fact that this is a global site, and many people do not use English as their primary language (and many English speaking people - especially those darn Yankees! - have no working knowledge of any other language); things can get very interesting.

    The impact is minimized (but definately not eliminated) when one has formally [meaning following an established, recognized path - not necessarilyin a "school"] and learns the accepted terms. VERY few on-line sites provide proper definitions for terms that are used, and a great many use improper terms.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; 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

  8. #8
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: object constructor scope

    Quote Originally Posted by TheCPUWizard View Post
    I think there is a difference between the two situations..
    I know it's not actually precise terminology, I just wanted to point out that it's a phrase that sometimes seems to manage to work up a heated debate somewhat out of proportion to it's 'incorrectness'.

    I'm off now to start a new thread where programmers will calmly and respectfully discuss why their coding style is best...
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured