Solved

Nested types

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

829 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