Solved

Nested types

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

803 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