Thread: A Question On Reference Type

  1. #31
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> A temporary lives until the end of the "full-expression", i.e. the next semicolon.
    That would mean that cpjust's example should work.

  2. #32
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    That line (in green) is completely useless since you're calling the default constructor twice. Just do this:
    I know that I am exactly calling constructor in different ways.
    [EDIT] Sorry you are right I thought you are saying something else.
    When constructor has no parameter we should use TestClass t; But not TestClass t();
    When for example it takes an int we should write TestClass t(intA);
    Why do you need a cast?
    QUOTE]What type cast?[/QUOTE]
    To cout << this; in constructor

    stack is a part of memory where local variables and arrays are stored in LIFO. It is also used for passing function parameters.
    Last edited by siavoshkc; 08-15-2008 at 02:13 PM.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  3. #33
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> To cout << this; in constructor
    I know. Use void*.

  4. #34
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    stack is a part of memory where local variables and arrays are stored in LIFO. It is also used for passing function parameters.
    Then you have answered your own question. t is a local (or perhaps more accurately in the context of the question, an automatic) variable.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #35
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    C++ has three kinds of storage: static, dynamic and automatic.

    They are typically mapped to the OS's .data/.bss, heap and stack.

    What kind of storage an object uses is generally very obvious:
    1) Globals, static class members and static locals are in static storage.
    2) Everything allocated by normal new, the malloc family, and std::get_temporary_buffer is in dynamic storage. There may be additional, implementation-specific means of obtaining memory in dynamic storage (e.g. HeapAlloc in Win32).
    3) Everything else - including temporaries, obviously - is automatic storage.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #36
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    I know. Use void*.
    Thanks. I should practice programming with extensions turned off or at least WL 4. Note the edit in my former post.
    But I still don't know why by calling constructor, class takes place in stack.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  7. #37
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    But I still don't know why by calling constructor, class takes place in stack.
    You're mistaking the cause here. The object is on the stack because it's not a global or on the heap. The constructor is called because it's an object.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #38
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    You're mistaking the cause here. The object is on the stack because it's not a global or on the heap. The constructor is called because it's an object.
    It is not anywhere. You mean by defining the class, it will be placed on the stack?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  9. #39
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No.
    You create it inside a function, so it isn't global.
    You don't create it with static, so it's not a static variable.
    You don't create it with new, so it's not on the heap.
    What's left? The stack! It's created on the stack.
    Everything that is not global, static or on the heap is placed on the stack!

    Class definition has nothing to do with it.
    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.

  10. #40
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You mean by defining the class, it will be placed on the stack?
    Careful with your terminology. This is a class definition:
    Code:
    class Foo
    {
     int m1;
    public:
      void bar();
    };
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #41
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Careful with your terminology. This is a class definition:
    I exactly mean it.

    By defining a class it is nowhere. It is just for compiler. If its true. How its constructor can be called?

    When we define a function in a file its code will be loaded into memory each time programs executes. But when its a member function of a class it is nowhere unless it has been defined static or the class has been instantiated.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  12. #42
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The functions (where the code lies) of the class is called the implementation.
    Also, it's true that the code in the class, the functions, are indeed created and put in memory, but what has that to do with the instance being created on the stack? Btw, all the member data is placed on the stack, but the function aren't, because they're code.
    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.

  13. #43
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    By defining a class it is nowhere. It is just for compiler. If its true. How its constructor can be called?
    It's called per instance.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using Vectors. MinGW warning
    By Viewer in forum C++ Programming
    Replies: 9
    Last Post: 03-26-2009, 03:15 PM
  2. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  3. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  4. question about .net to 6.0 change causing errors
    By jverkoey in forum C++ Programming
    Replies: 17
    Last Post: 03-23-2004, 10:45 AM
  5. Question type program for beginners
    By Kirdra in forum C++ Programming
    Replies: 7
    Last Post: 09-15-2002, 05:10 AM