A Question On Reference Type

This is a discussion on A Question On Reference Type within the C++ Programming forums, part of the General Programming Boards category; >> A temporary lives until the end of the "full-expression", i.e. the next semicolon. That would mean that cpjust's example ...

  1. #31
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> 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,231
    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,317
    >> To cout << this; in constructor
    I know. Use void*.

  4. #34
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,447
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,893
    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,231
    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,893
    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,231
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,450
    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,893
    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,231
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,450
    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,893
    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

Page 3 of 3 FirstFirst 123
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, 03:35 PM
  3. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 06: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, 09:45 AM
  5. Question type program for beginners
    By Kirdra in forum C++ Programming
    Replies: 7
    Last Post: 09-15-2002, 05:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21