Accesing private data from method of another object

This is a discussion on Accesing private data from method of another object within the C++ Programming forums, part of the General Programming Boards category; I define a class named MyString. Part of it follows: class MyString{ private: char *Str; int Length; public: MyString():Str(NULL),Length(0) {} ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    11

    Question Accesing private data from method of another object

    I define a class named MyString. Part of it follows:

    class MyString{
    private:
    char *Str;
    int Length;
    public:
    MyString():Str(NULL),Length(0) {}

    MyString(const MyString &rhs);

    ~MyString() {delete [] Str;}
    };


    Why am i allowed to use rhs's private variable rhs.Str from a method of another object? What am i doing wrong?


    MyString::MyString(const MyString &rhs)
    {
    Str = new char[rhs.Length +1];
    for(int i=0;i<=Length;i++) *(rhs.Str+i)= *(Str+i);
    cout<<"Copy Constructor Called...";
    }

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    101
    Why am i allowed to use rhs's private variable rhs.Str from a method of another object?
    Because you can access private members of a class from its member functions.
    - lmov

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    11
    Sorry, but i don't think i follow.

    if i have in my main function two instances of MyString:

    MyString strA,strB;

    and have a method in the class with the following prototype

    void func(MyString &x);

    You say that if i call in my main function strA.func(strB);

    its OK for strA.func(strB) to mess with strB's member variables???

    I am talking about two different instances of the class.

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    You are not explaining your problem sufficiently clearly I'm afraid. The example you have given originally is a copy constructor, if you can't access members when you make a copy, you could not make a copy!

    Your second message shows a completely different scenario.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    101
    if i have in my main function two instances of MyString:

    MyString strA,strB;

    and have a method in the class with the following prototype

    void func(MyString &x);

    You say that if i call in my main function strA.func(strB);

    its OK for strA.func(strB) to mess with strB's member variables???
    Yes, exactly. Private members of a class can only be accessed directly by member functions of that class.
    Code:
    #include <iostream>
    
    class X {
        int a_;
    public:
        X(int a) : a_(a) {}
        void func(X& x) { std::cout << x.a_ << std::endl; }
    };
    
    int main() {
        X x0(10), x1(20);
        x0.func(x1);
    }
    - lmov

  6. #6
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Best way to deal with that is to create a base class with protected data members. Then derive your other classes from the base - they will be able to access the protected members.

  7. #7
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    public/protected/private are based on the class, not the instance of this class. Any method of said class can access any property or method of it's arguments if they are of the same class.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  8. #8
    Seņor Member
    Join Date
    Jan 2002
    Posts
    561
    Don't you mean that all variables in a class and such have a scope of the whole class that it resides in? (I don't know if I understood it correctly)

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    11
    Ok, one last try and then give up on me. The following code is an example of what i am asking.

    #include <iostream.h>

    class ABC{

    private:
    int Data;
    public:
    ABC(int x): Data(x) {}
    int GetData() const {return Data;}
    void SetData(int x) {Data = x;}

    void func(ABC &x);
    };

    void ABC::func(ABC &x)
    {
    x.Data =34; // Why is this right ?
    // Shouldn't i be obliged to use x.SetData(34); That's my question.
    }


    int main()
    {
    ABC a(10),b(12);

    a.func(b);

    return 0;
    }

  10. #10
    Registered User
    Join Date
    Aug 2001
    Posts
    101
    void ABC::func(ABC &x)
    {
    x.Data =34; // Why is this right ?
    // Shouldn't i be obliged to use x.SetData(34); That's my question.

    }
    Because ABC::func and ABC::Data are both members of ABC (obviously). In other words you can access any private member of a class from its member functions and friends.
    - lmov

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. data structure design for data aggregation
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 05-20-2008, 06:43 AM
  2. Expert, Help please! MultiThreading in C++
    By dv007 in forum C++ Programming
    Replies: 10
    Last Post: 01-07-2006, 01:10 PM
  3. Post programs
    By GaPe in forum C# Programming
    Replies: 8
    Last Post: 05-12-2002, 11:07 AM
  4. Setting Object Data *double* For Class ComboBox :: MFC
    By kuphryn in forum C++ Programming
    Replies: 0
    Last Post: 03-24-2002, 05:20 PM
  5. Replies: 3
    Last Post: 12-03-2001, 12:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21