access to the methods of another class

Discussion in 'C++' started by Verbal Kint, Dec 20, 2006.

  1. Verbal Kint

    Verbal Kint Guest

    hi.
    could you please have a look to the following code:

    class cClassA
    {
    public:
    cClassA(void):itsX(0)
    {}
    void SetX (int val)
    {itsX = val;}
    int GetX ()
    {return itsX;}

    private:
    int itsX;
    };

    class cClassB
    {
    public:
    void AddDat (cClassA dat)
    {itsDat.push_back(dat);}
    //*/
    // alternative 1:
    int GetDat (int nr)
    {
    if (nr < (int)itsDat.size())
    return itsDat[nr].GetX();
    cout << "cClassB::GetDat\tPROBLEM" << endl;
    return -1;
    }
    void SetDat (int nr, int val)
    {
    if (nr < (int)itsDat.size()) itsDat[nr].SetX(val);
    cout << "cClassB::SetDat\tPROBLEM" << endl;
    }
    // alternative 2:
    cClassA* ModDat (int nr)
    {
    if (nr < (int)itsDat.size())
    return &itsDat[nr];
    cout << "cClassB::ModDat\tPROBLEM" << endl;
    }
    //*/
    private:
    vector<cClassA> itsDat;
    };

    int main()
    {
    int i;
    cClassA x;
    cClassB data;

    for (i=0;i<5;i++)
    {
    data.AddDat(x);
    }

    return 0;
    }

    my question is the following: which is the best way to get access to
    the methods of cClassA from cClassB? i have two alternatives here, but
    the problems are:
    1. alternative: if there are a lot more variables, hence a lot more
    methods, i have to write almost the same code in cClassB.
    2. alternative: after returning the pointer, e.g. to the main function,
    i don't know how to continue my code so that the data in itsDat will be
    modified permamently.

    do you have any idea, how to improve this code?

    THANKS for your help!
    V.K.
     
    Verbal Kint, Dec 20, 2006
    #1
    1. Advertising

  2. Verbal Kint

    Ian Collins Guest

    Verbal Kint wrote:
    > hi.
    > could you please have a look to the following code:
    >
    > class cClassA


    Odd naming convention.

    > {
    > public:
    > cClassA(void):itsX(0)


    drop the void in C++.

    > {}
    > void SetX (int val)
    > {itsX = val;}
    > int GetX ()
    > {return itsX;}
    >
    > private:
    > int itsX;
    > };
    >
    > class cClassB
    > {
    > public:
    > void AddDat (cClassA dat)


    Take care when passing by value, what you end up with is a copy of the
    class.

    > {itsDat.push_back(dat);}
    > //*/
    > // alternative 1:
    > int GetDat (int nr)
    > {
    > if (nr < (int)itsDat.size())


    Don't use naked casts.

    [snip]
    >
    > my question is the following: which is the best way to get access to
    > the methods of cClassA from cClassB? i have two alternatives here, but
    > the problems are:
    > 1. alternative: if there are a lot more variables, hence a lot more
    > methods, i have to write almost the same code in cClassB.


    A good sign your design is broken.

    > 2. alternative: after returning the pointer, e.g. to the main function,
    > i don't know how to continue my code so that the data in itsDat will be
    > modified permamently.
    >

    I'm not sure what you are saying, just use to pointer to call the accessors.

    --
    Ian Collins.
     
    Ian Collins, Dec 20, 2006
    #2
    1. Advertising

  3. Verbal Kint

    Verbal Kint Guest

    Dear Ian,

    thanks for the answer.
    > Odd naming convention.

    i just made this example for this post, so i wrote the first thing that
    came into my mind.

    > drop the void in C++.

    i thought that is more correct to add void. but i seem to be wrong.

    > Take care when passing by value, what you end up with is a copy of the class.

    YES, thats exactly what i intended.

    > Don't use naked casts.

    what do you mean? sorry, i dont understand that expression.

    THANKS.
    V.K.
     
    Verbal Kint, Dec 20, 2006
    #3
  4. Verbal Kint

    Jim Langston Guest

    "Verbal Kint" <> wrote in message
    news:...
    > Dear Ian,
    >
    > thanks for the answer.
    >> Odd naming convention.

    > i just made this example for this post, so i wrote the first thing that
    > came into my mind.
    >
    >> drop the void in C++.

    > i thought that is more correct to add void. but i seem to be wrong.
    >
    >> Take care when passing by value, what you end up with is a copy of the
    >> class.

    > YES, thats exactly what i intended.
    >
    >> Don't use naked casts.

    > what do you mean? sorry, i dont understand that expression.
    >
    > THANKS.
    > V.K.


    (int) x
    is a naked cast. What is it casting?
    static_cast<int>( x )
    is not a naked cast, we know what it's casting.

    That is, (int) could be casting a number of things.
    const_cast<int>( x )
    means x was probably a const int, and we want to use it as an int (very
    dangerous, make sure you know what you're doing when you use const_cast)
    There are reinterpret_cast which is very dangerous, able to treat any
    pointer as any other pointer (just about)
    dynamic_cast used on polymorphic types

    Instead of using any naked cast (char) (int) (unsigned int) (MyClass*)
    use the appropiate c++ cast
    static_cast<char> static_cast<int> const_cast<unsigned int>
    dynamic_cast<MyClass*>,
    whatever.

    It spells out exactly what is being cast, and helps you from casting too
    much
     
    Jim Langston, Dec 20, 2006
    #4
  5. Verbal Kint

    Salt_Peter Guest

    Verbal Kint wrote:
    > Dear Ian,
    >
    > thanks for the answer.
    > > Odd naming convention.

    > i just made this example for this post, so i wrote the first thing that
    > came into my mind.
    >
    > > drop the void in C++.

    > i thought that is more correct to add void. but i seem to be wrong.
    >
    > > Take care when passing by value, what you end up with is a copy of the class.

    > YES, thats exactly what i intended.


    No, thats exactly what you don't want. You want a reference,
    preferrably a const reference since you have no need to modify the
    parameter:

    class cClassB
    {
    std::vector< cClassA > itsDat;
    public:
    ...
    void AddDat (const cClassA& dat) {
    itsDat.push_back(dat);
    }
    ...
    };

    And incidentally, consider matching the interface of the underlying
    container. You'll find as you go along that there are many advantages
    to this technique.

    #include <vector>

    class A { };

    template< typename A >
    class B
    {
    std::vector< A > va;
    public:
    B(size_t sz = 0) : va(sz) { }
    B(const B& copy)
    {
    va = copy.va;
    }
    void push_back(const A& r_a) {
    va.push_back(r_a);
    }
    };

    int main()
    {
    B< A > b(1000); // one thousand As, instantly
    b.push_back( A() ); // 1001
    B< A > another(b); // copy 1001 elements
    }

    >
    > > Don't use naked casts.

    > what do you mean? sorry, i dont understand that expression.
    >
    > THANKS.
    > V.K.
     
    Salt_Peter, Dec 20, 2006
    #5
  6. Verbal Kint

    Bo Yang Guest

    Verbal Kint :
    > hi.
    > could you please have a look to the following code:
    >
    > class cClassA
    > {
    > public:
    > cClassA(void):itsX(0)
    > {}
    > void SetX (int val)
    > {itsX = val;}
    > int GetX ()
    > {return itsX;}
    >
    > private:
    > int itsX;
    > };
    >
    > class cClassB
    > {
    > public:
    > void AddDat (cClassA dat)
    > {itsDat.push_back(dat);}
    > //*/
    > // alternative 1:
    > int GetDat (int nr)
    > {
    > if (nr < (int)itsDat.size())
    > return itsDat[nr].GetX();
    > cout << "cClassB::GetDat\tPROBLEM" << endl;
    > return -1;
    > }
    > void SetDat (int nr, int val)
    > {
    > if (nr < (int)itsDat.size()) itsDat[nr].SetX(val);
    > cout << "cClassB::SetDat\tPROBLEM" << endl;
    > }
    > // alternative 2:
    > cClassA* ModDat (int nr)
    > {
    > if (nr < (int)itsDat.size())
    > return &itsDat[nr];
    > cout << "cClassB::ModDat\tPROBLEM" << endl;
    > }
    > //*/
    > private:
    > vector<cClassA> itsDat;
    > };
    >
    > int main()
    > {
    > int i;
    > cClassA x;
    > cClassB data;
    >
    > for (i=0;i<5;i++)
    > {
    > data.AddDat(x);
    > }
    >
    > return 0;
    > }
    >
    > my question is the following: which is the best way to get access to
    > the methods of cClassA from cClassB? i have two alternatives here, but
    > the problems are:
    > 1. alternative: if there are a lot more variables, hence a lot more
    > methods, i have to write almost the same code in cClassB.


    Whether you take this alternative or the following, when there are
    many variables in the class A, you should write many similar codes.
    Because the code are all accessors, which of course look similar.

    > 2. alternative: after returning the pointer, e.g. to the main function,
    > i don't know how to continue my code so that the data in itsDat will be
    > modified permamently.
    >

    I think the decision you made mostly depends on encapsulation and
    insulation.
    If you didn't want to other classes depends, you can just take the
    first one. If not, you can choose either I think~

    > do you have any idea, how to improve this code?
    >
    > THANKS for your help!
    > V.K.
    >
     
    Bo Yang, Dec 20, 2006
    #6
  7. Verbal Kint

    Verbal Kint Guest

    dear all,

    thanks a lot for your advices! i will try to include them into my
    program.

    regards,
    V.K.
     
    Verbal Kint, Dec 21, 2006
    #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. Davids
    Replies:
    1
    Views:
    560
    Eric Renken
    Nov 6, 2004
  2. E11
    Replies:
    1
    Views:
    4,895
    Thomas Weidenfeller
    Oct 12, 2005
  3. Steve Richter
    Replies:
    6
    Views:
    735
    Steve Richter
    May 3, 2007
  4. Oltmans
    Replies:
    6
    Views:
    370
    Terry Reedy
    Mar 11, 2009
  5. Kenneth McDonald
    Replies:
    5
    Views:
    353
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page