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

This is a discussion on What does a call to `some` constructor mean to the compiler ? within the C++ Programming forums, part of the General Programming Boards category; I suddenly thought of this, i.e. how ctors really work.. They are forbidden from having return types: --Then how does ...

  1. #1
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,523

    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
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,258
    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.
    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

  3. #3
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,523
    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)" ?
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,258
    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.
    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. #5
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,523
    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..
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,523
    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...

    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,258
    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.
    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

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,993
    >>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, 04:39 PM
  2. Replies: 6
    Last Post: 05-19-2010, 05:03 AM
  3. Call constructor
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2008, 03:16 AM
  4. C++ have a constructor call another constructor
    By QuestionC in forum C++ Programming
    Replies: 4
    Last Post: 05-17-2007, 02: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


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