Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question about protected variables 1

Status
Not open for further replies.

hylian90

MIS
Dec 14, 2004
16
US
Hey y'all. I have a question about protected variables within a class. Let's say you have a base capability class called BASE, and two derived classes, DERIVED1 and DERIVED2. Each of these derived classes has three protected variables. My question is this: Can DERIVED1 access DERIVED2's protected variables? To put it technically, can classes derived from the same base access each others' protected variables? Thanks! ::)

Brian



I have no idea what MIS means
 
Answer to your question: No

MIS=Management Information Systems
No idea what that means but it has nothing to do with management
 
Actually, yes it is possible. The mechanism is called class friendship. Have a look at the `friend' C++ keyword.
Here is a small example to illustrate how it works :
Code:
#include <iostream>

class Derived2;//forward declaration

class Derived1
{
  //routines of Derived2 can access private features of Derived1 :
  friend class Derived2;
protected:
  int _j;
};

class Derived2
{
public:
  // A dummy access to Derived1::_j.
  void please_do_it (Derived1& d1)
  {
    std::cout << "d1._j = " << d1._j << std::endl;
  }

protected:
  int _i;
};


//Little test.
int main ()
{
  Derived1 d1;
  Derived2 d2;

  d2.please_do_it (d1);
  return 0;
};

--
Globos
 
Actually, that's rather ironic, considering the two classes are called "player" and "enemy." You would think they'd be anything but friendly.
Enemy -->[hammer]<-- Player

--------------------------------------
Code:
//Program to tick off my sister
Programming is officialy a life skill

I have no idea what MIS means
 
1. It's not the best style: all capitals in class names; traditioanally it's reserved for macros names...
2. Derived1 is NOT a kind of Derived2. It's not the best design (it's the worst design;): to access D1 protected members from D2 (and vice versa).
3. About last note above: yes, it's funny, but it's a good design tip - don't do that (enemy as a friend of player and vice versa;)...
 
About the caps, I just did that so it would be easier to see the actual class names from the type.

As for two . . . I'm working on that. But I need members of enemy to be able to access members of player so that they can reassign their HP variable.

Three . . . Just making an observation

--------------------------------------
Code:
//Program to tick off my sister
Programming is officialy a life skill

I have no idea what MIS means
 
Suggestion would be to make mutator functions in the base class that both classes then inherit.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top