pointers and assignment operators, basic question

Discussion in 'C++' started by Jeff Bender, Jun 2, 2007.

  1. Jeff Bender

    Jeff Bender Guest

    I am trying to remember how to code in C++ after many years of using
    Java exclusively.

    I have this setup:

    class Base {
    public:
    virtual void printA(){}
    };

    class D1 : public Base {
    public:
    D1() {
    a = 1;
    }
    int a;
    void printA() {cout << a << endl;}
    };

    class D2 : public Base {
    public:
    D2() {
    a = 2;
    }
    int a;
    void printA() {cout << a << endl;}
    };

    int main() {
    D1 * d1 = new D1;
    D2 * d2 = new D2;
    Base * b1 = d1;
    Base * b2 = d2;
    *b1 = *b2; // HERE What does this _do_?
    b1->printA();
    return 0;
    }

    The program, as _you_ would expect, outputs 1. _I_ am trying to
    figure out why it doesn't output 2.

    What does the line marked HERE do? I expected it to overwrite the
    memory that starts at b1 with the memory that starts at b2, but that
    is clearly not the case or the output would be 2. If I do something
    like this:

    int * a = new int(3);
    int * b = new int(5);
    *a = *b;

    the memory starting at a was overwritten by the contents of the memory
    in b. Why is it different in the situation above? What am I
    missing? Thanks for helping me to remember this stuff.
     
    Jeff Bender, Jun 2, 2007
    #1
    1. Advertising

  2. Jeff Bender

    Andre Kostur Guest

    Jeff Bender <> wrote in
    news::

    > I am trying to remember how to code in C++ after many years of using
    > Java exclusively.
    >
    > I have this setup:
    >
    > class Base {
    > public:
    > virtual void printA(){}
    > };
    >
    > class D1 : public Base {
    > public:
    > D1() {
    > a = 1;
    > }
    > int a;
    > void printA() {cout << a << endl;}
    > };
    >
    > class D2 : public Base {
    > public:
    > D2() {
    > a = 2;
    > }
    > int a;
    > void printA() {cout << a << endl;}
    > };
    >
    > int main() {
    > D1 * d1 = new D1;
    > D2 * d2 = new D2;
    > Base * b1 = d1;
    > Base * b2 = d2;
    > *b1 = *b2; // HERE What does this _do_?


    Nothing. It attempts to call Base::eek:perator=() and passing it a Base& to
    *b2, which by default does a memberwise assignment of the Base portion.
    Since Base has no members, it does nothing.

    > b1->printA();
    > return 0;
    > }
    >
    > The program, as _you_ would expect, outputs 1. _I_ am trying to
    > figure out why it doesn't output 2.
    >
    > What does the line marked HERE do? I expected it to overwrite the
    > memory that starts at b1 with the memory that starts at b2, but that
    > is clearly not the case or the output would be 2. If I do something
    > like this:


    Uh, no. It does memberwise assignment, not a memcpy.

    > int * a = new int(3);
    > int * b = new int(5);
    > *a = *b;
    >
    > the memory starting at a was overwritten by the contents of the memory
    > in b. Why is it different in the situation above? What am I
    > missing? Thanks for helping me to remember this stuff.


    No, this assigns an int to an int. There's a difference between:

    *a = *b;

    and

    memcpy(a, b, sizeof(*a));

    (Granted, not much different for an int, could be _wildly_ different for
    a class.)
     
    Andre Kostur, Jun 2, 2007
    #2
    1. Advertising

  3. Jeff Bender

    Sarath Guest

    On Jun 2, 8:17 am, Jeff Bender <> wrote:
    > I am trying to remember how to code in C++ after many years of using
    > Java exclusively.
    >
    > I have this setup:
    >
    > class Base {
    > public:
    > virtual void printA(){}
    >
    > };
    >
    > class D1 : public Base {
    > public:
    > D1() {
    > a = 1;
    > }
    > int a;
    > void printA() {cout << a << endl;}
    >
    > };
    >
    > class D2 : public Base {
    > public:
    > D2() {
    > a = 2;
    > }
    > int a;
    > void printA() {cout << a << endl;}
    >
    > };
    >
    > int main() {
    > D1 * d1 = new D1;
    > D2 * d2 = new D2;
    > Base * b1 = d1;
    > Base * b2 = d2;
    > *b1 = *b2; // HERE What does this _do_?
    > b1->printA();
    > return 0;
    >
    > }
    >
    > The program, as _you_ would expect, outputs 1. _I_ am trying to
    > figure out why it doesn't output 2.
    >
    > What does the line marked HERE do? I expected it to overwrite the
    > memory that starts at b1 with the memory that starts at b2, but that
    > is clearly not the case or the output would be 2. If I do something
    > like this:
    >
    > int * a = new int(3);
    > int * b = new int(5);
    > *a = *b;
    >
    > the memory starting at a was overwritten by the contents of the memory
    > in b. Why is it different in the situation above? What am I
    > missing? Thanks for helping me to remember this stuff.


    The assignment really invokes the operator= function generated by the
    compiler, which does a bitwise copy.

    Copy constructor, operator=, default constructor, destructor functions
    will be generated by compiler if we do not write our own.

    Regards.
    Sarath
     
    Sarath, Jun 2, 2007
    #3
  4. Jeff Bender wrote:
    > I am trying to remember how to code in C++ after many years of using
    > Java exclusively.
    >
    > I have this setup:
    >
    > class Base {
    > public:
    > virtual void printA(){}
    > };
    >
    > class D1 : public Base {
    > public:
    > D1() {
    > a = 1;
    > }
    > int a;
    > void printA() {cout << a << endl;}
    > };
    >
    > class D2 : public Base {
    > public:
    > D2() {
    > a = 2;
    > }
    > int a;
    > void printA() {cout << a << endl;}
    > };
    >
    > int main() {
    > D1 * d1 = new D1;
    > D2 * d2 = new D2;
    > Base * b1 = d1;
    > Base * b2 = d2;
    > *b1 = *b2; // HERE What does this _do_?
    > b1->printA();
    > return 0;
    > }
    >
    > The program, as _you_ would expect, outputs 1. _I_ am trying to
    > figure out why it doesn't output 2.
    >
    > What does the line marked HERE do? I expected it to overwrite the
    > memory that starts at b1 with the memory that starts at b2, but that
    > is clearly not the case or the output would be 2. If I do something
    > like this:
    >
    > int * a = new int(3);
    > int * b = new int(5);
    > *a = *b;
    >
    > the memory starting at a was overwritten by the contents of the memory
    > in b. Why is it different in the situation above? What am I
    > missing? Thanks for helping me to remember this stuff.
    >


    I think you are missing that operator= is not virtual. So because you
    call it with Base object all you are doing is calling the Base::eek:perator=.

    john
     
    John Harrison, Jun 2, 2007
    #4
  5. Jeff Bender

    terminator Guest

    On Jun 2, 3:17 am, Jeff Bender <> wrote:
    > I am trying to remember how to code in C++ after many years of using
    > Java exclusively.
    >
    > I have this setup:
    >
    > class Base {
    > public:
    > virtual void printA(){}
    >
    > };
    >
    > class D1 : public Base {
    > public:
    > D1() {
    > a = 1;
    > }
    > int a;
    > void printA() {cout << a << endl;}
    >
    > };
    >
    > class D2 : public Base {
    > public:
    > D2() {
    > a = 2;
    > }
    > int a;
    > void printA() {cout << a << endl;}
    >
    > };
    >
    > int main() {
    > D1 * d1 = new D1;
    > D2 * d2 = new D2;
    > Base * b1 = d1;
    > Base * b2 = d2;
    > *b1 = *b2; // HERE What does this _do_?
    > b1->printA();
    > return 0;
    >
    > }
    >
    > The program, as _you_ would expect, outputs 1. _I_ am trying to
    > figure out why it doesn't output 2.
    >
    > What does the line marked HERE do? I expected it to overwrite the
    > memory that starts at b1 with the memory that starts at b2, but that
    > is clearly not the case or the output would be 2. If I do something
    > like this:
    >
    > int * a = new int(3);
    > int * b = new int(5);
    > *a = *b;
    >
    > the memory starting at a was overwritten by the contents of the memory
    > in b. Why is it different in the situation above? What am I
    > missing? Thanks for helping me to remember this stuff.


    assume:

    int *a;
    int *b;

    remark1:

    a=b;//this means that from now on 'a' points to where 'b' used to
    point.
    /*now a==b*/
    remark2:

    *a=1;//sets the value of the memmory location pointed to by 'a' to 1
    *a=*b;/*sets the value of the memmory location pointed to by 'a' to
    the value stored at the memmory location pointed to by 'b' .In general
    this means that 'a!=b' but '*a==*b' for a while(unless you modify 'a'
    or 'b') */

    > What does the line marked HERE do? I expected it to overwrite the


    It does not care what 'b1' and 'b2' actually point to.It just copies a
    'Base' object(not a 'D1' nor a 'D2') from the location pointed to by
    'b2' to the location pointed to by 'b1' .

    > figure out why it doesn't output 2.



    since the type of 'b1' is still considered to be 'Base' nothing is
    printed to the output.

    > *b1 = *b2; // HERE What does this _do_?


    try this one:

    b1 = b2;//now the next line prints 2 to the output.


    remark3:

    the meaning of '*' in pointer delarations and pointer-cast operators
    differs from the meaning of '*' in dereferencing statements.

    regards,
    FM
     
    terminator, Jun 2, 2007
    #5
  6. Jeff Bender

    BobR Guest

    Jeff Bender <> wrote in message
    news:...
    > I am trying to remember how to code in C++ after many years of using
    > Java exclusively.
    > I have this setup:
    >
    > class Base { public:
    > virtual void printA(){}
    > };
    >
    > class D1 : public Base { public:
    > D1() {
    > a = 1;
    > }
    > int a;
    > void printA() {cout << a << endl;}
    > };


    You should prefer 'initializer lists' (vs. assignment) in constructors:

    class D1 : public Base { public:
    D1() : a( 1 ){} // note the colon
    int a;
    void printA(){ cout << a << endl;}
    };

    --
    Bob R
    POVrookie
     
    BobR, Jun 2, 2007
    #6
  7. Jeff Bender

    Jeff Bender Guest

    Everyone, thanks a lot for your help. I understand now. I had also
    completely forgotten about initializer lists, but I knew something
    about my constructor didn't look right.

    On Jun 2, 12:42 pm, "BobR" <> wrote:
    > Jeff Bender <> wrote in message
    >
    > news:...
    >
    > > I am trying to remember how to code in C++ after many years of using
    > > Java exclusively.
    > > I have this setup:

    >
    > > class Base { public:
    > > virtual void printA(){}
    > > };

    >
    > > class D1 : public Base { public:
    > > D1() {
    > > a = 1;
    > > }
    > > int a;
    > > void printA() {cout << a << endl;}
    > > };

    >
    > You should prefer 'initializer lists' (vs. assignment) in constructors:
    >
    > class D1 : public Base { public:
    > D1() : a( 1 ){} // note the colon
    > int a;
    > void printA(){ cout << a << endl;}
    > };
    >
    > --
    > Bob R
    > POVrookie
     
    Jeff Bender, Jun 8, 2007
    #7
    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. David Rasmussen

    Composite Assignment Operators for enums

    David Rasmussen, Feb 24, 2004, in forum: C++
    Replies:
    3
    Views:
    870
    Thomas Matthews
    Feb 24, 2004
  2. Jakob Bieling

    Q: c'tor and assignment operators

    Jakob Bieling, Jan 16, 2005, in forum: C++
    Replies:
    1
    Views:
    390
    Rolf Magnus
    Jan 16, 2005
  3. Replies:
    25
    Views:
    702
    Steve Holden
    May 18, 2006
  4. Anthony Paul

    short-circuit evaluation and assignment operators

    Anthony Paul, Jun 6, 2009, in forum: C Programming
    Replies:
    5
    Views:
    1,311
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    695
Loading...

Share This Page