Solved

Nested types

Posted on 2000-02-26
5
269 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
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now