Advertisement

Stupid Newbie Question #1

Started by November 14, 2000 10:28 AM
13 comments, last by BriarLoDeran 24 years, 2 months ago
Here goes: What I want to do seems simple enough, but I don''t know how exactly. Let use define the following classes for demonstrative purposes:

class One
{
  public:
  int x;
  int doubleNumber(int a)
  {
    return (a * 2);
  }
};

class Two
{
  public:
  int y;
  int doubleNumberPlusOne(int b);
}
 
Ok. I want to be able to use the doubleNumber function in both the following ways:

int Two::doubleNumberPlusOne(int b)
{
  // doubleNumber() is from class One
  return (doubleNumber(b) + 1);
}

// also, I want to be able to do this:

Two t1;
int x = t1.doubleNumber(5);
 
Is this clear? I want a class to be able to use another class''s functions. Now I don''t mean just to do the same thing, but to actually call the function from within the class. The only way I could figure to do this is by making a function pointer in the Two class and then make it equal the function in One, but there must be a better way to do this. Is this where virtual functions come in? ...because I don''t know anything about them. Oh right, and I don''t simply want class Two to inherent class One, I just want to use one of its functions. I hope this makes sense... Thanks in advance, Briar LoDeran
I don''t think it works this way.

One::doubleNumber(int a)is a member of class One and has no access to Two::doubleNumberPlusOne(int b) unless you use the friend specifier which in that case can use it as if Two::doubleNumberPlusOne(int b) was a meber one One.
Advertisement
I thought a friend function allowed the class to access it''s "friends" private members.
Your best bet is probably inheritance.
Firstly... Why do you want to do this?

If the function doesnt operate on any of the data in class One then take it out of the class and put it into an included file (e.g. stdafx). then just include the file in both classes. If it does operate on some of Class One''s data, then write a function in class two with the same name that expects a variable of type Class One as a parameter then just calls the function in Class one with the other params..

i.e


Two::DoSomethingInOne (int param1, One* ClassOne) {
ClassOne->DoSomethingInOne(param1)
}
NightWraith
Not sure about that anonymous but i agree that inheritance would be as good as any other way.
or just make the call like this

return One::DoubleNumber(b);

And BTW, virtual functions use inheritance.
Advertisement
Ok, so this was a poor example =(. Let''s say we make the following change:
int doubleNumber(int a){  // now mult by x instead of 2...  return (a * x);} 

This is closer to what I want done. The function does work on the data in class One.

Will that make a difference?

Briar LoDeran
My question is why not just put DoubleNumber into class Two... that way you keep it OOP
You mean use the variable x which is a member of One right?
If that is so, then just use the member function of One that you''ve created to access x in the member function of Two.

if you don''t get it, just take a look here:

One::doubleNumber(int a)
{
// now mult by x instead of 2...
return (a * ObjectOfTwo.GetMemberX());
}

this would return a multiplies with the number returned by

Two::GetMemberX();
{
return x;
}
I still say the easiest way is inheritance... Otherwise you could try something silly like this

Class Two
{
public:
int y;
int DoubleNumberPlusOne(int b);
private:
One m_one;
}

int Class Two::DoubleNumberPlusOne(int b)
{
return m_one.DoubleNumer(b);
}

This topic is closed to new replies.

Advertisement