By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,136 Members | 1,021 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,136 IT Pros & Developers. It's quick & easy.

access to the methods of another class

P: n/a
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<cClassAitsDat;
};

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.

Dec 20 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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.
Dec 20 '06 #2

P: n/a
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.

Dec 20 '06 #3

P: n/a
"Verbal Kint" <TE********@gmail.comwrote in message
news:11*********************@80g2000cwy.googlegrou ps.com...
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<charstatic_cast<intconst_cast<unsigned int>
dynamic_cast<MyClass*>,
whatever.

It spells out exactly what is being cast, and helps you from casting too
much
Dec 20 '06 #4

P: n/a

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.
Dec 20 '06 #5

P: n/a
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<cClassAitsDat;
};

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.
Dec 20 '06 #6

P: n/a
dear all,

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

regards,
V.K.

Dec 21 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.