No. The former declares a function named t that takes no arguments and returns a Test. The latter is not allowed, as noted by MarkZWEERS.
No. The former declares a function named t that takes no arguments and returns a Test. The latter is not allowed, as noted by MarkZWEERS.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
How the constructor is called without using 'new'? Is it implicity called when you call the constructor? It seems by calling the constructor weare implicity using 'new', am I right?
Would you explain more, please?The latter is not allowed, as noted by MarkZWEERS.
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
Whenever you create an object, one of the object's constructors is implicitly called. Even the use of new does not explicitly call a constructor (and I believe that applies even to placement new).How the constructor is called without using 'new'? Is it implicity called when you call the constructor?
No.It seems by calling the constructor weare implicity using 'new', am I right?
Attempt to compile this program:Would you explain more, please?
Code:class Test {}; int main() { Test &t = Test(); }
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
This code compiled and ran successfully:
[edit]Code:#include <iostream> using namespace std; class Test { public: Test() { cout << "Test" << (int)this << endl; } ~Test() { cout << "~Test" << endl; } void test(){cout <<"fun"<<endl;} }; int main() { Test& t = Test(); t.test(); cout << "main" << endl; return 0; }
Where did we create an object inWhenever you create an object
?Code:Test();
Last edited by siavoshkc; 08-15-2008 at 01:17 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
Disable compiler extensions. Your code should not compile on a standard compliant implementation.This code compiled and ran successfully:
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Yes now it is giving me two errorsDisable compiler extensions.
What is this extension?delme.cpp(8) : error C2440: 'type cast' : cannot convert from 'Test *const ' to 'int'
The target is not large enough
delme.cpp(15) : error C2440: 'initializing' : cannot convert from 'Test' to 'Test &'
A non-const reference may only be bound to an lvalue
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
Visual C++ is a known compiler to allow this extension. If you go to warnings level 4, you should also receive a warning saying non-standard extension used.
The extension allows you to bind a non-const reference to a temporary, which is disallowed by the standard (only const references can bind to temporaries, which has been pointed out earlier).
OK. When we writeWhat happens? Does a memory allocation take place for class on heap or it is just a temp on stack (or somewhere else)? Why it can be bind to const ref but not non-const?Code:Test()
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
Unless you use new, it's on the stack. Always the stack, unless it's new.
I believe it's because you shouldn't be able to modify a temporary because it's just that - a temporary. Its lifetime is less than that of the variable it's bound to. Bad things would happen if you try to modify it after its lifetime has expired (while the lifetime of the variable holding the reference is still alive).
When I disabled extension it generates error for my type cast. So how should I cast that?
Second this code works fine:Why by calling constructor it takes place in stack?Code:class Test { public: Test() { cout << "Test" << endl; } ~Test() { cout << "~Test" << endl; } void test(){cout <<"fun"<<endl;} }; int main() { Test(); Test t = Test(); t.test(); cout << "main" << endl; return 0; }
[EDIT]And what about thisrt is static in stack in file scope. Right?Code:class Test { public: Test() { cout << "Test" << endl; } ~Test() { cout << "~Test" << endl; } void test(){cout <<"fun"<<endl;} }rt;
Last edited by siavoshkc; 08-15-2008 at 01:57 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
That's one thing that has burned me a number of times... What exactly is the lifetime of a temporary object? It's obviously less than a regular variable which ends when it gets to its closing brace.
I think this is one of the cases where I've gotten burned:
In the case above, I believe the temporary object doesn't even life until the end of the function call.Code:std::string Func() { ... } int main() { ... AnotherFunc( Func().c_str() ); ... }
What type cast?When I disabled extension it generates error for my type cast. So how should I cast that?
What is the stack?Why by calling constructor it takes place in stack?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
>> When I disabled extension it generates error for my type cast. So how should I cast that?
Why do you need a cast there? Just output this (or cast to void*).
Temporaries passed to a function live until the function ends, but in cpjust's example the result of Func() isn't passed to a function, a pointer is. So the result of Func() lives until c_str() is called and it's value is sent to the function. Then it can be destroyed.
A temporary lives until the end of the "full-expression", i.e. the next semicolon. Unless, that is, it is bound to a reference, in which case its lifetime is extended to that of the reference.
In particular, a temporary appearing with function parameters lives until the full expression containing the function call finishes. However, if the function keeps a reference to the object in question, this reference will soon be invalid.
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