DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: copy constructor syntax

  1. #1
    aniruddha Guest

    copy constructor syntax


    The syntax of copy constructor goes like this say for e.g class A;
    A(A & atype)
    {
    member=atype.member;
    }

    Is it ok if we have the same constructor defined like this
    A(A * atype)
    {
    member=atype->member;
    }

    Will this be still called a copy constructor .Essentially the difference
    is that we are passing the parameter by pointer rather than by reference.

    Can anyone help please!!!



  2. #2
    Randy Charles Morin Guest

    Re: copy constructor syntax

    The second constructor is a copy constructor.
    It will not work with syntax like
    A a;
    A b = a;
    You might run into some unintended behaviors as this may cause implicit
    casts when using pointers.
    --
    Randy Charles Morin
    Author of Programming Windows Services
    http://www.kbcafe.com

    "aniruddha" <aniruddhard@hotmail.com> wrote in message
    news:3b1b6692$1@news.devx.com...
    >
    > The syntax of copy constructor goes like this say for e.g class A;
    > A(A & atype)
    > {
    > member=atype.member;
    > }
    >
    > Is it ok if we have the same constructor defined like this
    > A(A * atype)
    > {
    > member=atype->member;
    > }
    >
    > Will this be still called a copy constructor .Essentially the difference
    > is that we are passing the parameter by pointer rather than by reference.
    >
    > Can anyone help please!!!
    >
    >




  3. #3
    Spooky Guest

    Re: copy constructor syntax


    If the syntax
    A b = a
    doesnt work, then how does the second constructor become a copy constructor.
    Syntactically copy constructors must accept a single argument of reference
    to the same class type. There can be more arguments to it, but thatz a different
    story.

    "Randy Charles Morin" <rmorin@kbcafe.com> wrote:
    >The second constructor is a copy constructor.
    >It will not work with syntax like
    >A a;
    >A b = a;
    >You might run into some unintended behaviors as this may cause implicit
    >casts when using pointers.
    >--
    >Randy Charles Morin
    >Author of Programming Windows Services
    >http://www.kbcafe.com
    >
    >"aniruddha" <aniruddhard@hotmail.com> wrote in message
    >news:3b1b6692$1@news.devx.com...
    >>
    >> The syntax of copy constructor goes like this say for e.g class A;
    >> A(A & atype)
    >> {
    >> member=atype.member;
    >> }
    >>
    >> Is it ok if we have the same constructor defined like this
    >> A(A * atype)
    >> {
    >> member=atype->member;
    >> }
    >>
    >> Will this be still called a copy constructor .Essentially the difference
    >> is that we are passing the parameter by pointer rather than by reference.
    >>
    >> Can anyone help please!!!
    >>
    >>

    >
    >



  4. #4
    Danny Kalev Guest

    Re: copy constructor syntax

    no. a copy ctor may have only one of these four forms:

    A(A& a);
    A(const A& a);
    A(volatile A& a);
    A(const volatile A& a);

    Any other parameter type (e.g., a pointer to A) would make it an
    ordinary ctor, not a copy ctor.

    Danny


    aniruddha wrote:
    >
    > The syntax of copy constructor goes like this say for e.g class A;
    > A(A & atype)
    > {
    > member=atype.member;
    > }
    >
    > Is it ok if we have the same constructor defined like this
    > A(A * atype)
    > {
    > member=atype->member;
    > }
    >
    > Will this be still called a copy constructor .Essentially the difference
    > is that we are passing the parameter by pointer rather than by reference.
    >
    > Can anyone help please!!!


  5. #5
    gurvinder Guest

    Re: copy constructor syntax


    hi aniruddha
    see why copy ctor is there at first place...
    problem is faced whenm large object is to passed as parameter..and we know
    that argument are stored on stack from right to left and return values
    are stored in registers..but objects are generally large so we can't store
    then in registers..so idea came up to pass the reference of the object
    as the argument and assign it's return value to some other object .SO i think
    we can't use pointers instead of reference....
    bye
    Gurvinder


    "aniruddha" <aniruddhard@hotmail.com> wrote:
    >
    >The syntax of copy constructor goes like this say for e.g class A;
    >A(A & atype)
    > {
    > member=atype.member;
    > }
    >
    >Is it ok if we have the same constructor defined like this
    >A(A * atype)
    > {
    > member=atype->member;
    > }
    >
    >Will this be still called a copy constructor .Essentially the difference
    >is that we are passing the parameter by pointer rather than by reference.
    >
    >Can anyone help please!!!
    >
    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center