Private members and copy constructors

Discussion in 'C++' started by Full Decent, Aug 6, 2005.

  1. Full Decent

    Full Decent Guest

    aclass A, B;

    Can A access B's privates all the time, or only in a copy constructor?

    -------and---------

    This code:

    #include <iostream>
    class A
    {
    int my;
    public:
    A():my(0){}
    void print(){std::cout<<my<<std::endl;}
    void test(A x){x.my++;}
    };

    int main()
    {
    A a, b;
    a.test(b);
    a.print();
    b.print();
    }

    Produces "0 0" when run.

    Expected "0 1" or compile error.

    --------------------

    Please tell me this is one of those staying-up-late mistakes :)

    Thanks,
    Will
    Full Decent, Aug 6, 2005
    #1
    1. Advertising

  2. Full Decent wrote:
    > aclass A, B;
    >
    > Can A access B's privates all the time, or only in a copy constructor?


    All the time.

    > -------and---------
    >
    > This code:
    >
    > #include <iostream>
    > class A
    > {
    > int my;
    > public:
    > A():my(0){}
    > void print(){std::cout<<my<<std::endl;}


    I strongly recommend rewriting it at least as

    void print(ostream& os) const { os << my; }

    > void test(A x){x.my++;}


    Passing an object by value makes a _copy_. Changing that _copy_
    inside the function has no effect (in your case) on the object
    from which the copy was made. Pass it by reference:

    void test(A& x) { x.my++; }

    > };
    >
    > int main()
    > {
    > A a, b;
    > a.test(b);
    > a.print();
    > b.print();


    And doing here

    a.print(std::cout); std::cout << std::endl;
    b.print(std::cout); std::cout << std::endl;

    > }
    >
    > Produces "0 0" when run.


    Rightfully so.

    > Expected "0 1" or compile error.


    I wouldn't.

    > --------------------
    >
    > Please tell me this is one of those staying-up-late mistakes :)


    Or one of those I-don't-understand-the-difference-between-passing-
    by-value-and-passing-by-reference mistakes...

    V
    Victor Bazarov, Aug 6, 2005
    #2
    1. Advertising

  3. Full Decent

    Peter Julian Guest

    "Full Decent" <> wrote in message
    news:...
    > aclass A, B;
    >
    > Can A access B's privates all the time, or only in a copy constructor?


    What B?

    >
    > -------and---------
    >
    > This code:
    >
    > #include <iostream>
    > class A
    > {
    > int my;
    > public:
    > A():my(0){}
    > void print(){std::cout<<my<<std::endl;}
    > void test(A x){x.my++;}
    > };


    test() creates a local instance of class A called x using a pass_by_value /
    copy ctor.

    >
    > int main()
    > {
    > A a, b;
    > a.test(b);
    > a.print();
    > b.print();
    > }
    >
    > Produces "0 0" when run.
    >
    > Expected "0 1" or compile error.


    No, instance b was never modified in any way. Only a local copy in test was
    modified.

    >
    > --------------------
    >
    > Please tell me this is one of those staying-up-late mistakes :)
    >
    > Thanks,
    > Will
    >


    Try:

    #include <iostream>

    class A
    {
    int n;
    public:
    A():n(0) {}
    void print() const {std::cout << n << std::endl;}
    void test() {n++;}
    };

    int main()
    {
    A a, b;
    a.test();
    b.test();
    a.print();
    b.print();
    }
    Peter Julian, Aug 6, 2005
    #3
  4. Full Decent

    Full Decent Guest

    I can't believe I made this mistake in the original code and still
    passed by value in this dumb example.

    Luckily you saved me and I can stay up later to make dumber mistakes.
    Full Decent, Aug 6, 2005
    #4
  5. Full Decent

    Full Decent Guest

    Peter, the idea was to do this:

    #include <iostream>
    class A
    {
    int my;
    public:
    A():my(0){}
    void print(){std::cout<<my<<std::endl;}
    void test(A& x){x.my++;}

    };

    int main()
    {
    A a, b;
    a.test(b);
    a.print();
    b.print();
    }

    This code proves that a can modify b's private parts. I wasn't sure if
    this was allowed.
    Full Decent, Aug 6, 2005
    #5
  6. Full Decent

    Ram Guest

    > This code proves that a can modify b's private parts. I wasn't sure if
    > this was allowed.


    The access restrictions apply on a per class basis and not on a per
    object one. So objects belonging to the same class can always access
    each other's members whether public, protected or private..

    -Ramashish
    Ram, Aug 6, 2005
    #6
  7. Full Decent

    Vijai Kalyan Guest

    I wouldn't think all the time. Consider the following code:

    template<class T>
    class Foo
    {
    int x;
    public:
    template<class C> Foo(const Foo<C>& src)
    {
    x = src.x;
    }
    };

    I don't think you will be allowed to access src's privates. I guess
    that is because a generic class is really a standin for a new type?
    Vijai Kalyan, Aug 7, 2005
    #7
  8. Vijai Kalyan wrote:
    > I wouldn't think all the time. Consider the following code:
    >
    > template<class T>
    > class Foo
    > {
    > int x;
    > public:
    > template<class C> Foo(const Foo<C>& src)
    > {
    > x = src.x;
    > }
    > };
    >
    > I don't think you will be allowed to access src's privates. I guess
    > that is because a generic class is really a standin for a new type?


    That's correct. Unless 'C' is the same as 'T', 'Foo<C>' is not the
    same type as 'Foo<T>'. Another twist here: the templated "copy-
    constructor" cannot replace or even overload the "true" copy-c-tor,
    generated in this case by the compiler for the Foo<T> class when it
    is instantiated. So, no matter what, inside the constructor template
    'C' will simply _never_ be the same as 'T'. Weird, eh?

    V
    Victor Bazarov, Aug 7, 2005
    #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. CoolPint
    Replies:
    8
    Views:
    957
    Jeff Schwab
    Dec 14, 2003
  2. Jeremy Smith
    Replies:
    2
    Views:
    574
    Jeremy Smith
    Aug 3, 2006
  3. Jess
    Replies:
    5
    Views:
    586
    Ron Natalie
    Jun 7, 2007
  4. thomas
    Replies:
    3
    Views:
    380
    Martin York
    Feb 16, 2008
  5. srp113
    Replies:
    3
    Views:
    457
Loading...

Share This Page