Friendship or inheritance problem

How can I allow class one to use a function in class two?
1
2
3
4
5
6
7
8
9
10
class one
    {
        public:
        void sayone(){cout << "Class one\n\n";}
    };
    class two : public one
    {
        public:
        void saytwo(){cout << "Class two\n\n";}
    };

Example:
1
2
one ObjectOne;
ObjectOne.saytwo();


Edit:
And on a side note...
What in tarnation( :P ) is this.foo or foo* this ...? I'm sure I am geting it wrong. I think I have seen either *this or this* though. WHAT IS this ! It' freaking driving me crazy.
Last edited on
Neither inheritance nor friendship works like that. If two inherits from one, two is an extension of one, not the other way around. Friendship only gives access to private and protected members of a class, it does not allow you to use these members as if they were your own.
Of course, if you made two the parent and one the child, it would be okay.

What's a "tarnation"? Anyway, this is a pointer to the object for witch the method was called. As with all pointers, *this is the object this points to and you can access members with this->member. I've heard that some compilers let you use this as a reference instead. That is, you'd access members with this.member. This is not standard though.
So, what are the advantages/uses to using a this pointer. And if you don't mind...How would I declare a class method with this ?

I guess I will try if you promise not to laugh...

1
2
3
4
5
6
7
8
9
10
11
12
class theClass
{
public:
void this->GetMethod(){cout << "Test\n\n";}
//or
void this<GetMethod>(){cout << "Test\n\n";}
};
int main()
{
theClass obj;
obj this->GetMethod();
}


Okay...so I have no idea. But could you give me a very simple and basic(Not the language basic) example based off of mine..if it's even possible? I'd really appreciate it. The this pointer has been driving me nuts for years. It sounds like it could be very useful, though.

BTW: Thanks for the above answer.
Consider an example
1
2
3
4
5
6
7
8
struct Foo {
   int field;
   void method();
};

void function(Foo* ptr) {
   std::cout << ptr->field;
}
Now imagine that you need 'method' to call 'function'. What do you pass it? This is why this is needed.
1
2
3
void Foo::method() {
   function(this);//no other way to do it.
}
I'm having dificulty understanding this. (Pun intended) Your saying that if a non member function takes a class as a parameter(I can't remember the word for it) then a method of the class cannot not call it? Unless you do this function(this);. Or is the pointer the problem and not what I pass into the function.
The concept of the this pointer is difficult for beginners to understand.

Member functions are assumed to be acting on an object. Here's a simple example:

1
2
3
4
5
6
7
8
9
10
class Person
{
public:
  int myAge;  // typically this wouldn't be public, but whatever.  It's an example

  int GetAge()
  {
    return myAge;
  }
};


Now, when you use this class, you need to have actual objects. Each object has it's own copy of the information. In this case, you can have multiple Persons, and each Person has their own age:

1
2
3
4
5
Person Bill;
Bill.myAge = 20;  // Bill is 20 years old

Person Bob;
Bob.myAge = 30;  // Bob is 30 


It doesn't make any sense at all for you to use myAge without having an object to go with:

1
2
Person::myAge = 40;  //  ?  wtf?  Whose age is 40?  This makes no sense
 //  compiler error! 


It's the exact same idea when you call a member function. The function needs an object on which to act:

1
2
3
4
5
// Use with an object.  OK
int BobsAge = Bob.GetAge();  // get Bob's age

// Use with no object.  Nonsense - compiler error
int WhoseAge = Person::GetAge();  // nonsense!  whose age are we trying to get? 



The this pointer is used in the context of the member function to represent the object being acted on. For an example of this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class ThisExample
{
public:
  void PrintThis()
  {
    cout << this << endl;
  }
};

//...

int main()
{
  // make a few objects
  ThisExample a;
  ThisExample b;

  // Let's see what the address of 'a' is:
  cout << &a << endl;

  // When we call a member function of ThisExample, 'this' becomes a pointer to the object
  //  which we are acting on.
  a.PrintThis();  // here, since we are acting on 'a'.  'this' will point to 'a' inside of the PrintThis function
  // so PrintThis will print the exact same thing we just printed when we printed &a


  // if we call PrintThis using the 'b' object, it will print a different address, because we will have
  //  a different this pointer
  b.PrintThis();  // this time, 'this' will point to 'b', not 'a'
}




As for how this is used, most of the time you don't need to worry about it. It's mostly used internally. The only time you really need it is when you need to have 2-way communication between classes.

Here's a simple example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// We have a game that has enemies that fire bullets.  We need to keep track of which enemy fired
// which bullet for scoring purposes or something

class Enemy;


class Bullet
{
private:
  Enemy* myOwner;  // the 'owner' of this bullet.  IE:  the enemy who fired it

public:
  Bullet( Enemy* owner )  // constructor
  {
    myOwner = owner;
    // ...
  }
};

class Enemy
{
public:
  void FireBullet()
  {
    Bullet* bullet = new Bullet( this );  // Make a new bullet.  Indicate that 'this'
      // Enemy object is the one that created it
  }
};

int main()
{
  Enemy frodo;
  frodo.FireBullet();  // this will spawn a new bullet.  That bullet's 'myParent' pointer will point to 'frodo'
    // since frodo is the one that fired it.
}
I'm guessing I should hold off on this for a while. There are a few thing in your explanation that i am unfamiliar with. I will read through your explanation a few times and keep it in mind for later use. thanks a lot, once again, for your help Disch.
I think that's a rather good explanation
MSDN wrote:
The this pointer is a pointer accessible only within the nonstatic member functions of a class, struct, or union type. It points to the object for which the member function is called. Static member functions do not have a this pointer.


'this' is often used when there's a name clash
1
2
3
4
void func(int var)
{
  this->var = var; // this makes clear that 'var' left of '=' is a member variable while right of '=' is the variable passed to the function
}
Thank you, coder777. That helped simplify this for me. I will look into some reference material on the subject for further study. Again, thanks to everyone for their help. this isn't as alien an concept as it was before. :)
Topic archived. No new replies allowed.