Thread: A Question On Reference Type

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    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

  2. #17
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    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?

    The latter is not allowed, as noted by MarkZWEERS.
    Would you explain more, please?
    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. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    How the constructor is called without using 'new'? Is it implicity called when you call the constructor?
    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).

    It seems by calling the constructor weare implicity using 'new', am I right?
    No.

    Would you explain more, please?
    Attempt to compile this program:
    Code:
    class Test {};
    
    int main()
    {
        Test &t = Test();
    }
    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

  4. #19
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    This code compiled and ran successfully:
    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;
    }
    [edit]
    Whenever you create an object
    Where did we create an object in
    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

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This code compiled and ran successfully:
    Disable compiler extensions. Your code should not compile on a standard compliant implementation.
    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

  6. #21
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Disable compiler extensions.
    Yes now it is giving me two errors
    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
    What is this extension?
    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. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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).
    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.

  8. #23
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    OK. When we write
    Code:
    Test()
    What 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?
    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. #24
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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).
    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. #25
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    When I disabled extension it generates error for my type cast. So how should I cast that?
    Second this code works fine:
    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;
    }
    Why by calling constructor it takes place in stack?

    [EDIT]And what about this
    Code:
    class Test {
    public:
        Test() { cout << "Test"  << endl; }
        ~Test() { cout << "~Test" << endl; }
          void test(){cout <<"fun"<<endl;}
    }rt;
    rt is static in stack in file scope. Right?
    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

  11. #26
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    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:
    Code:
    std::string Func() { ... }
    
    int main()
    {
       ...
       AnotherFunc( Func().c_str() );
       ...
    }
    In the case above, I believe the temporary object doesn't even life until the end of the function call.

  12. #27
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    When I disabled extension it generates error for my type cast. So how should I cast that?
    What type cast?

    Why by calling constructor it takes place in stack?
    What is the stack?
    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

  13. #28
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by siavoshkc View Post
    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;
    }
    Why by calling constructor it takes place in stack?
    That line (in green) is completely useless since you're calling the default constructor twice. Just do this:
    Code:
    Test t;

  14. #29
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> 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.

  15. #30
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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

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