>> A temporary lives until the end of the "full-expression", i.e. the next semicolon.
That would mean that cpjust's example should work.
>> A temporary lives until the end of the "full-expression", i.e. the next semicolon.
That would mean that cpjust's example should work.
I know that I am exactly calling constructor in different ways.That line (in green) is completely useless since you're calling the default constructor twice. Just do this:
[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);
QUOTE]What type cast?[/QUOTE]Why do you need a cast?
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
>> To cout << this; in constructor
I know. Use void*.
Then you have answered your own question. t is a local (or perhaps more accurately in the context of the question, an automatic) variable.stack is a part of memory where local variables and arrays are stored in LIFO. It is also used for passing function parameters.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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
Thanks. I should practice programming with extensions turned off or at least WL 4. Note the edit in my former post.I know. Use void*.
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
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.But I still don't know why by calling constructor, class takes place in stack.
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
It is not anywhere. You mean by defining the class, it will be placed on the 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.
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
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.
Careful with your terminology. This is a class definition:You mean by defining the class, it will be placed on the stack?
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
I exactly mean it.Careful with your terminology. This is a class definition:
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
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.
It's called per instance.By defining a class it is nowhere. It is just for compiler. If its true. How its constructor can be called?
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