Thread: What does a call to `some` constructor mean to the compiler ?

  1. #1
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657

    What does a call to `some` constructor mean to the compiler ?

    I suddenly thought of this, i.e. how ctors really work..
    They are forbidden from having return types:
    --Then how does the following code work?
    Code:
    class X
    {
            public:
            X(int){};
    };
    int main()
    {
            X x(4);
            x = X(47); //Assuming that = is well defined or the default works
            return 0;
    }
    Would it be correct to assume that the compiler(or at least some of them) implement constructors so that they 'return' the constructed object ?
    Can anyone point me to a document that has some evidence on it or says that it is false?
    *I got hold of the gcc/g++ sources but have no idea how to look for the needle within the hay ...and the readme's are not very explicit

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by manasij7479
    how does the following code work?
    X(47) constructs a temporary X object which is then assigned to x.

    Quote Originally Posted by manasij7479
    Would it be correct to assume that the compiler(or at least some of them) implement constructors so that they 'return' the constructed object ?
    The reason for a constructor is to provide a way to construct an object, so all constructors 'return' the constructed object.
    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

  3. #3
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    The reason for a constructor is to provide a way to construct an object, so all constructors 'return' the constructed object.
    That is almost exactly what I said to my teachers, but they didn't seem to get the point. Could you say say something more substantial ...and with some tangible proof ?

    X(47) constructs a temporary X object
    ..then what is the difference between calling X(int) and Y(int) ..which could have the prototype "X Y(int)" ?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by manasij7479
    That is almost exactly what I said to my teachers, but they didn't seem to get the point. Could you say say something more substantial ...and with some tangible proof ?
    What do you want me to say, and what do you want me to prove? I am not going to prove that constructors return objects, because they don't, and I don't need to prove that constructors 'return' objects because that is obviously true.

    Quote Originally Posted by manasij7479
    ..then what is the difference between calling X(int) and Y(int) ..which could have the prototype "X Y(int)" ?
    Y is just a function that returns an object of type X: it could be designated to return an X object with initial values, or not. The purpose of the constructor, on the other hand, is to construct objects of type X, and in this it can initialise the members of the X object.
    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. #5
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    I am not going to prove that constructors return objects, because they don't, and I don't need to prove that constructors 'return' objects because that is obviously true.
    'return' as in.. generate a lvalue that is a valid object of the class in question ? right ?
    By proof I meant, some standard document or rather unreasonably(sorry for that) source code of the implementations , saying that they should or does..in the later case..

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I think you are chasing a wild goose or a red herring here.

    How do you want to define "return" (notice laserlight uses quotes to render this figurative)? If you want it defined in a technical way that demonstrates ctors, under the hood, do essentially the same thing as any other function, you might find one that loosely does prove this, but I guarantee there will be a more stringent definition that demonstrates, technically, it is not the same event because there are distinct differences.

    I would take "return" to refer to the actual keyword. Since a ctor cannot have the keyword return in it, when we say a ctor returns an object, it is not meant literally, because constructors do not use "return".

    WRT you argument in class, let it go.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    WRT you argument in class, let it go.
    .Ok.

    I would take "return" to refer to the actual keyword. Since a ctor cannot have the keyword return in it, when we say a ctor returns an object, it is not meant literally, because constructors do not use "return".
    I meant 'return' in a figurative way too...but your statement about the possibility of a more stringent definition convinces me...


  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by manasij7479
    'return' as in.. generate a lvalue that is a valid object of the class in question ? right ?
    No, since a constructor may construct an object that is not an lvalue. For example, see the second note at C++03 Clause 12.1 Paragraph 13.
    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

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >>x = X(47);
    The way this works is simple...

    Construct a temporary object tmp1 and invoke the constructor X(int).
    Finally, invoke operator = on object x with the right-hand side object tmp1.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specialized Constructor call Default Constructor
    By threahdead in forum C++ Programming
    Replies: 15
    Last Post: 08-23-2010, 03:39 PM
  2. Replies: 6
    Last Post: 05-19-2010, 04:03 AM
  3. Call constructor
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2008, 02:16 AM
  4. C++ have a constructor call another constructor
    By QuestionC in forum C++ Programming
    Replies: 4
    Last Post: 05-17-2007, 01:59 AM
  5. Call constructor within constructor. How to?
    By xErath in forum C++ Programming
    Replies: 10
    Last Post: 11-18-2004, 05:30 PM

Tags for this Thread