|
-
Aug 16th, 2001, 05:28 PM
#1
Thread Starter
Addicted Member
Nested Classes
how do you access the inner class in a nested class through the outer class
PHP Code:
class Game{
public:
class Ball{
i have the class game and the inner class ball; i want too access the Ball member functions through the class Game like this.
Game.Ball.Member_Function();
-
Aug 16th, 2001, 06:17 PM
#2
Lively Member
You need to create an instance of Ball in Game. When all you have is the definition the most you can do is define a instance of ball from game, like this
Code:
Game g;
g.Ball b;
b.MemberFunction();
In order to use ball in game you must declare a ball object to use like this.
Code:
class Game
{
public:
Game();
~Game();
class Ball
{
public:
void Member_Function();
}
Ball b;
}
Game g;
g.b.Member_Function();
-
Aug 16th, 2001, 06:28 PM
#3
I'm curious - what's the reason for having a nested class? I'd just do:
Code:
class Ball
{
...
void myFunc();
...
};
class Game
{
...
Ball myBall;
...
};
and something like:
void main() // or wherever...
{
Game myGame;
...
// this way, you can call it like you want:
myGame.myBall.myFunc();
...
}
Destined
-
Aug 16th, 2001, 07:27 PM
#4
Thread Starter
Addicted Member
No reason; i just thought that you could use the ball nested class as a member of Game;
-
Aug 17th, 2001, 02:13 PM
#5
If you have a nested class you do not _have_ to declare it within the at class. If it is defined as public you can declare an instance of the nested class anywhere you want...
class MyClass
{
public:
class Nested
{};
};
int main()
{
MyClass::Nested nestedTest;
return 1;
}
This works...
-
Aug 17th, 2001, 04:16 PM
#6
Strange, isn't this entirely defeating the purpose? Why not just do:
Code:
class Ball{};
class Game
{
Ball b;
};
Isn't this the same as your example, amac?
Does anyone know what nesting a class allows or does? From my point of view, it just seems to be a more readable coding style to declare them seperately.
Although, it has been about 3 years since I've actually read through a book on C++, so I may have once known the answer.
Destined
-
Aug 21st, 2001, 06:23 AM
#7
I am not exactly sure here but it makes sense...
You might be able to define the class as being private/protected...
therefore it can only be used with the class it was defined... if i am wrong here someone correct me?
I guess it is just another level of encapsulation...
-
Aug 21st, 2001, 06:28 AM
#8
an example:
class Base
{
private:
class Nested
{};
protected:
Nested Ball;
};
int main()
{
Base test; // would declare an object
// of Nested inside it...
Base::Nested test2; // error.
return 1;
}
-
Aug 27th, 2001, 01:40 AM
#9
The STL containers use it.
All iterators are nested classes, because although they have similar interfaces(++-operator and so on), they need different implementations(you can't cycle a double linked list the same way as an array).
Code:
template <class T>
class vektor
{
public:
class iterator
{
int pos;
public:
T Next() {return vektor[++pos];};
T Prev() {return vektor[--pos];};
}
}
template <class T>
class list
{
class node
{
node* prev;
T value;
node* next;
friend class list::iterator;
}
public:
class iterator
{
list::node *currentNode;
public:
T Next() {currentNode = currentNode->next; return currentNode};
T Prev() {currentNode = currentNode->prev; return currentNode};
}
}
(of course, this is only an expectation)
As you see, vektor::iterator and list::iterator behvae the same, although they have different implementations.
All the buzzt
CornedBee
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|