Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Nested types

Posted on 2000-02-26
5
Medium Priority
?
290 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 60 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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

876 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