Copy Constructor with Pointer instead of Reference

Discussion in 'C++' started by Mohan, Oct 17, 2006.

  1. Mohan

    Mohan Guest

    Hi,

    What are the advantages/disadvantages of using a pointer instead of
    Reference in the Copy Constructor ?

    For Example,

    Writing the Copy constructor for the Class "Temp" as below,

    Temp(const base *ptrBase)
    {
    }

    instead of
    Temp(const base &objBase)
    {
    }


    Thanks & Regards,
    Mohan
     
    Mohan, Oct 17, 2006
    #1
    1. Advertising

  2. * Mohan:
    > What are the advantages/disadvantages of using a pointer instead of
    > Reference in the Copy Constructor ?
    >
    > For Example,
    >
    > Writing the Copy constructor for the Class "Temp" as below,
    >
    > Temp(const base *ptrBase)
    > {
    > }
    >
    > instead of
    > Temp(const base &objBase)
    > {
    > }


    None of these are copy constructors.

    Consult your nearest C++ textbook to learn what a copy constructor is.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Oct 17, 2006
    #2
    1. Advertising

  3. Mohan

    Guest

    In case of copy constructor if we use donot use reference in that case
    it will create an object and call the copy constructor and passing the
    value to the copy constructor and each time a new object is created and
    each time it will call the copy constructor it goes to infinite and
    fill the memory then it display the error message .


    if we pass the reference it will not create the new object for storing
    the value. and no recursion will take place






    Mohan wrote:
    > Hi,
    >
    > What are the advantages/disadvantages of using a pointer instead of
    > Reference in the Copy Constructor ?
    >
    > For Example,
    >
    > Writing the Copy constructor for the Class "Temp" as below,
    >
    > Temp(const base *ptrBase)
    > {
    > }
    >
    > instead of
    > Temp(const base &objBase)
    > {
    > }
    >
    >
    > Thanks & Regards,
    > Mohan
     
    , Oct 17, 2006
    #3
  4. Mohan

    Mohan Guest

    Thanks for the reply,

    I am sorry, it was a copy/paste problem

    Temp(const Temp *ptrBase)
    {
    }

    My intention is to know will there be performace or any other issues ??



    "Alf P. Steinbach" <> wrote in message
    news:...
    >* Mohan:
    > > What are the advantages/disadvantages of using a pointer instead of
    >> Reference in the Copy Constructor ?
    >>
    >> For Example,
    >>
    >> Writing the Copy constructor for the Class "Temp" as below,
    >>
    >> Temp(const base *ptrBase)
    >> {
    >> }
    >>
    >> instead of
    >> Temp(const base &objBase)
    >> {
    >> }

    >
    > None of these are copy constructors.
    >
    > Consult your nearest C++ textbook to learn what a copy constructor is.
    >
    > --
    > A: Because it messes up the order in which people normally read text.
    > Q: Why is it such a bad thing?
    > A: Top-posting.
    > Q: What is the most annoying thing on usenet and in e-mail?
     
    Mohan, Oct 17, 2006
    #4
  5. Mohan

    Mohan Guest

    Thanks for the reply,

    I am sorry, it was a copy/paste problem

    Temp(const Temp *ptrBase)
    {
    }

    My intention is to know will there be performace or any other issues in
    using a Ponter instead of reference in Copy constructor??



    "Alf P. Steinbach" <> wrote in message
    news:...
    >* Mohan:
    > > What are the advantages/disadvantages of using a pointer instead of
    >> Reference in the Copy Constructor ?
    >>
    >> For Example,
    >>
    >> Writing the Copy constructor for the Class "Temp" as below,
    >>
    >> Temp(const base *ptrBase)
    >> {
    >> }
    >>
    >> instead of
    >> Temp(const base &objBase)
    >> {
    >> }

    >
    > None of these are copy constructors.
    >
    > Consult your nearest C++ textbook to learn what a copy constructor is.
    >
    > --
    > A: Because it messes up the order in which people normally read text.
    > Q: Why is it such a bad thing?
    > A: Top-posting.
    > Q: What is the most annoying thing on usenet and in e-mail?
     
    Mohan, Oct 17, 2006
    #5
  6. Mohan

    Mohan Guest

    Thanks for the reply,

    I am sorry, it was a copy/paste problem

    Temp(const Temp *ptrBase)
    {
    }

    My intention is to know will there be performace or any other issues in
    using a Ponter instead of reference in Copy constructor??



    "Alf P. Steinbach" <> wrote in message
    news:...
    >* Mohan:
    > > What are the advantages/disadvantages of using a pointer instead of
    >> Reference in the Copy Constructor ?
    >>
    >> For Example,
    >>
    >> Writing the Copy constructor for the Class "Temp" as below,
    >>
    >> Temp(const base *ptrBase)
    >> {
    >> }
    >>
    >> instead of
    >> Temp(const base &objBase)
    >> {
    >> }

    >
    > None of these are copy constructors.
    >
    > Consult your nearest C++ textbook to learn what a copy constructor is.
    >
    > --
    > A: Because it messes up the order in which people normally read text.
    > Q: Why is it such a bad thing?
    > A: Top-posting.
    > Q: What is the most annoying thing on usenet and in e-mail?
     
    Mohan, Oct 17, 2006
    #6
  7. * Mohan:
    > Thanks for the reply,
    >
    > I am sorry, it was a copy/paste problem
    >
    > Temp(const Temp *ptrBase)
    > {
    > }
    >
    > My intention is to know will there be performace or any other issues ??
    >
    >
    >
    > "Alf P. Steinbach" <> wrote in message
    > news:...
    >> * Mohan:
    >> > What are the advantages/disadvantages of using a pointer instead of
    >>> Reference in the Copy Constructor ?
    >>>
    >>> For Example,
    >>>
    >>> Writing the Copy constructor for the Class "Temp" as below,
    >>>
    >>> Temp(const base *ptrBase)
    >>> {
    >>> }
    >>>
    >>> instead of
    >>> Temp(const base &objBase)
    >>> {
    >>> }

    >> None of these are copy constructors.
    >>
    >> Consult your nearest C++ textbook to learn what a copy constructor is.
    >>
    >> --
    >> A: Because it messes up the order in which people normally read text.
    >> Q: Why is it such a bad thing?
    >> A: Top-posting.
    >> Q: What is the most annoying thing on usenet and in e-mail?

    >
    >
    >


    Please don't top-post. Please don't quote signatures. Please see my
    original answer quoted somewhere above (it still applies, after the code
    correction).

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Oct 17, 2006
    #7
  8. Mohan

    David W Guest

    "Mohan" <> wrote in message news:eh1qi0$vcg$...
    > Thanks for the reply,
    >
    > I am sorry, it was a copy/paste problem
    >
    > Temp(const Temp *ptrBase)
    > {
    > }
    >
    > My intention is to know will there be performace or any other issues ??


    On performance, it's up to the machine and the compiler, but very likely there would be no
    difference at all. On other issues, a reference would be more convenient than a pointer in most
    cases, and absolutely essential in others, since your pointer version is not a copy constructor.
    Whether to copy from a pointer or a reference just isn't an issue. AFAIK, everyone uses a reference
    without even thinking.

    DW
     
    David W, Oct 17, 2006
    #8
    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. Aire
    Replies:
    3
    Views:
    486
    Mike Wahler
    Jan 25, 2004
  2. Replies:
    11
    Views:
    1,036
    Nindi
    Nov 14, 2006
  3. Generic Usenet Account
    Replies:
    10
    Views:
    2,341
  4. cinsk
    Replies:
    35
    Views:
    2,725
    James Kanze
    Oct 11, 2010
  5. A
    Replies:
    7
    Views:
    656
Loading...

Share This Page