access to the methods of another class

V

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.
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.
 
I

Ian Collins

Verbal said:
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.
 
V

Verbal Kint

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.
 
J

Jim Langston

Verbal Kint said:
Dear Ian,

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

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

YES, thats exactly what i intended.

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
 
S

Salt_Peter

Verbal said:
Dear Ian,

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

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

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

Bo Yang

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~
 
V

Verbal Kint

dear all,

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

regards,
V.K.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top