Solved

Nested types

Posted on 2000-02-26
5
275 Views
Last Modified: 2010-04-10
Have anyone tried to nest different types within a class?

What I know works:
  class within class
  enum within class

I'm asking about:
  structs within class
  typedef within class
  other type nestings

Anyone?
0
Comment
Question by:wy2lam
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 

Expert Comment

by:YuraPhink
ID: 2560733
structs within class - is the same as class within class. the difference is that struct has public variables by default.
typedefs is the same.
So what is the problem,or may be I didn't understood your question?
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2560740
wy2lam:
You can...

W.Yinan
0
 
LVL 10

Accepted Solution

by:
RONSLOW earned 20 total points
ID: 2560761
You can next classes, structs, enums, typedefs within a class or struct.

They become local to the class.  You need a scope modifier to get at them externally.  They also respect access keywords (like private:).

eg.

class A {
public:
  class B {};
};

You refer to class B, from outside of class A, as A::B.  If I had not put 'public:' in there, A::B would be private and could not be used elsewhere.

Typedefs are useful things to put into classes. some examples are

class A : public B {
  tytpedef B BaseClass;
  ...
}

Then you can use 'BaseClass' rather than remember that class A is derived from B.  This can be useful in templated class definitions.

ATL uses typedefs in classes for many of its classes .. such as binary operator classes.

enums within a class are a good way of defining named constants without the names themselves having to be globally unique.

eg.
class A {
  enum { XXX = 1 };
};
class B {
  enum { XXX = 2 };
};

A:XXX and B:XXX are separate values.
0
 

Author Comment

by:wy2lam
ID: 2561618
About the struct thing, I know it is a class, just don't know if it is treated otherwise *identically* with classes by the compiler.

And the typedef's within a class is a nice trick - does the scope of the typedef matter?  i.e. can I write

class A : public B {
public:
    typedef B BaseClass;
....
};

and refer to A::Baseclass from outside codes?
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 2561784
Classes are struct, struct are classes. The only difference is the default access.  structs default to public, classes to private.

And, yes, you can refer to A:Baseclass.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question