A Class within another Class
I was just wondering if you could define a class within the definition of another class. If it is possibly can someone show some example code. Thanks.
Go on an Intense Rampage
Sure you can (not a really useful example, though):
This will print
A::foo()
A::B::foo()
A::B::foo()
Of course, the usual access rules (public, private, protected) also hold for the inner class (B in this case).
HTH
|
This will print
A::foo()
A::B::foo()
A::B::foo()
Of course, the usual access rules (public, private, protected) also hold for the inner class (B in this case).
HTH
Some useful C++ links:Free multiplatform ANSI C++ Standard Library implementationVisual C++ 6.0 STL fixesVisual C++ 6.0 noncompliance issuesC++ FAQ Lite
I''m not sure I understand the question. If the question is "Can you use classes within classes, as data members, etc?" the answer is certainly, and let us know and we''ll give you details regarding how. If the question is "Can I define more than one class in a header file?" then the answer is yes, simply add another class block and make sure you put the right class name to the functions. If you''re curious about deriving classes from other classes to use the functionality of one class in another class with more functionality, we can give you some resources on that as well.
-fel
-fel
~ The opinions stated by this individual are the opinions of this individual and not the opinions of her company, any organization she might be part of, her parrot, or anyone else. ~
June 06, 2001 03:58 PM
Sure
class Wheel {
public:
//whatever one needs to make a wheel.
private:
};
class Car {
public:
private:
Wheel wheels[4];
};
Something like that. When making classes, just ask yourself "is
this a has-a relationship or an is-a relationship?" For example,
a car has-a wheel (actually 4 of them, usually) and a Mustang
is-a car... So Mustang would be inherited from Car while Car
possesses 4 Wheels.
Get it?
class Wheel {
public:
//whatever one needs to make a wheel.
private:
};
class Car {
public:
private:
Wheel wheels[4];
};
Something like that. When making classes, just ask yourself "is
this a has-a relationship or an is-a relationship?" For example,
a car has-a wheel (actually 4 of them, usually) and a Mustang
is-a car... So Mustang would be inherited from Car while Car
possesses 4 Wheels.
Get it?
My class looks like this:
class CMap
{
public:
int XMax;
int YMax;
int TileWidth;
int TileHeight;
class CCamera
{
public:
int X;
int Y;
void Initialize(void)
{
X = 0;
Y = 0;
}
};
};
I want to create a variable in a .h file using extern then I declare the variable in a .cpp
I keep getting error that say the CMap is undefined.
class CMap
{
public:
int XMax;
int YMax;
int TileWidth;
int TileHeight;
class CCamera
{
public:
int X;
int Y;
void Initialize(void)
{
X = 0;
Y = 0;
}
};
};
I want to create a variable in a .h file using extern then I declare the variable in a .cpp
I keep getting error that say the CMap is undefined.
Go on an Intense Rampage
Out of curiosity, what is the reasoning behind putting class CCamera in the middle of class CMap?
Also, why use extern rather than forward class reference?
Perhaps if you explained what you''re doing, we could give you an easier approach to it.
-fel
Also, why use extern rather than forward class reference?
Perhaps if you explained what you''re doing, we could give you an easier approach to it.
-fel
~ The opinions stated by this individual are the opinions of this individual and not the opinions of her company, any organization she might be part of, her parrot, or anyone else. ~
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement