Question about passing variables to a function

Discussion in 'C++' started by Jeroen, Mar 30, 2007.

  1. Jeroen

    Jeroen Guest

    Hi,

    Assume a function which returns an object of type 'my_class':

    my_class get_class()
    {
    ...
    }

    Now I have the following question:

    void my_function(my_class m)
    {
    ...
    }

    my_function(get_class());

    Question: in this call to my_function, is the copy constructor of
    my_class called to make a copy of the object returned by get_class? Or
    can a smart compiler skip that step? At first glance I would think that
    the copy contructor must be called because a copy of my_class must be
    created on the stack when calling my_function...

    A similar question is about the following code:

    my_class my_function()
    {
    ...
    return get_class();
    }

    Is a copy constructor called to copy the object returned by get_class?
    Or can a smart compiler skip that step?

    Thanx for any answers,

    Jeroen
     
    Jeroen, Mar 30, 2007
    #1
    1. Advertising

  2. Jeroen

    mimi Guest

    On 3ÔÂ30ÈÕ, ÏÂÎç4ʱ55·Ö, Jeroen <> wrote:
    > Hi,
    >
    > Assume a function which returns an object of type 'my_class':
    >
    > my_class get_class()
    > {
    > ...
    > }
    >
    > Now I have the following question:
    >
    > void my_function(my_class m)
    > {
    > ...
    > }
    >
    > my_function(get_class());
    >
    > Question: in this call to my_function, is the copy constructor of
    > my_class called to make a copy of the object returned by get_class? Or
    > can a smart compiler skip that step? At first glance I would think that
    > the copy contructor must be called because a copy of my_class must be
    > created on the stack when calling my_function...
    >
    > A similar question is about the following code:
    >
    > my_class my_function()
    > {
    > ...
    > return get_class();
    > }
    >
    > Is a copy constructor called to copy the object returned by get_class?
    > Or can a smart compiler skip that step?
    >
    > Thanx for any answers,
    >
    > Jeroen


    For primitive types, most modern compilers would skip the step.You
    could use the smart to ease your life.But for user-define types,you
    should not rely on your compiler's possible optimization.
    You could write a simple program to test your compiler's intelligence
    quotient.
    #include <iostream>
    class Foo {
    public:
    Foo()
    {
    }
    Foo(const Foo &f)
    {
    std::cout << "Copy constructor for Foo\n";
    }
    };

    Foo test(Foo f)
    {
    return Foo();
    }

    int main()
    {
    test(Foo());
    return 0;
    }
     
    mimi, Mar 30, 2007
    #2
    1. Advertising

  3. Jeroen

    Gavin Deane Guest

    On 30 Mar, 09:55, Jeroen <> wrote:
    > Hi,
    >
    > Assume a function which returns an object of type 'my_class':
    >
    > my_class get_class()
    > {
    > ...
    > }
    >
    > Now I have the following question:
    >
    > void my_function(my_class m)
    > {
    > ...
    > }
    >
    > my_function(get_class());
    >
    > Question: in this call to my_function, is the copy constructor of
    > my_class called to make a copy of the object returned by get_class? Or
    > can a smart compiler skip that step? At first glance I would think that
    > the copy contructor must be called because a copy of my_class must be
    > created on the stack when calling my_function...
    >
    > A similar question is about the following code:
    >
    > my_class my_function()
    > {
    > ...
    > return get_class();
    > }
    >
    > Is a copy constructor called to copy the object returned by get_class?
    > Or can a smart compiler skip that step?


    The compiler is allowed to elide the copy in cases like this, so the
    copy constructor might not be called. However, even if the copy is
    elided, the copy constructor must still be accessible. If my_class
    were not copyable (e.g. copy constructor declared private) then the
    code would not compile, regardless of whether the compiler would elide
    the copy or not.

    Gavin Deane
     
    Gavin Deane, Mar 30, 2007
    #3
  4. Jeroen

    Jeroen Guest

    Gavin Deane schreef:
    > On 30 Mar, 09:55, Jeroen <> wrote:
    >> Hi,
    >>
    >> Assume a function which returns an object of type 'my_class':
    >>
    >> my_class get_class()
    >> {
    >> ...
    >> }
    >>
    >> Now I have the following question:
    >>
    >> void my_function(my_class m)
    >> {
    >> ...
    >> }
    >>
    >> my_function(get_class());
    >>
    >> Question: in this call to my_function, is the copy constructor of
    >> my_class called to make a copy of the object returned by get_class? Or
    >> can a smart compiler skip that step? At first glance I would think that
    >> the copy contructor must be called because a copy of my_class must be
    >> created on the stack when calling my_function...
    >>
    >> A similar question is about the following code:
    >>
    >> my_class my_function()
    >> {
    >> ...
    >> return get_class();
    >> }
    >>
    >> Is a copy constructor called to copy the object returned by get_class?
    >> Or can a smart compiler skip that step?

    >
    > The compiler is allowed to elide the copy in cases like this, so the
    > copy constructor might not be called. However, even if the copy is
    > elided, the copy constructor must still be accessible. If my_class
    > were not copyable (e.g. copy constructor declared private) then the
    > code would not compile, regardless of whether the compiler would elide
    > the copy or not.
    >
    > Gavin Deane
    >


    Hi Gavin,

    Thanks for answering. Does your answer "The compiler is allowed to elide
    the copy in cases like this" refer to both cases I described? So, also
    to the first case "my_function(get_class());"?

    Thanks,

    Jeroen
     
    Jeroen, Mar 30, 2007
    #4
  5. Jeroen

    anon Guest

    Jeroen wrote:
    > Assume a function which returns an object of type 'my_class':
    >
    > my_class get_class()
    > {
    > ...
    > }
    >
    > Now I have the following question:
    >
    > void my_function(my_class m)
    > {
    > ...
    > }
    >
    > my_function(get_class());


    The copy constructor will be called in this case

    >
    > Question: in this call to my_function, is the copy constructor of
    > my_class called to make a copy of the object returned by get_class? Or
    > can a smart compiler skip that step? At first glance I would think that
    > the copy contructor must be called because a copy of my_class must be
    > created on the stack when calling my_function...
    >
    > A similar question is about the following code:
    >
    > my_class my_function()
    > {
    > ...
    > return get_class();
    > }
    >


    The copy-constructor does not have to be called in this case - it
    depends on compiler you using
     
    anon, Mar 30, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QnJhZA==?=

    Another simple question - passing variables

    =?Utf-8?B?QnJhZA==?=, Apr 3, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    420
    Steve C. Orr [MVP, MCSD]
    Apr 4, 2005
  2. James Vanns
    Replies:
    7
    Views:
    7,167
    Evan Carew
    Jan 21, 2004
  3. Ninan
    Replies:
    6
    Views:
    506
    kanze
    Jun 24, 2006
  4. Replies:
    3
    Views:
    394
    Eric Sosman
    Jul 20, 2007
  5. Replies:
    9
    Views:
    1,007
Loading...

Share This Page