Nested class question...
In Bjarne Stroustrup''s C++ Programming Language, I saw something that looked like this:
class Something
{
public:
class SomethingElse;
};
So, how do you define and implement SomethingElse? Like this:
class Something::SomethingElse
{
...
}
???
I don''t have a clue...
farmersckn
Yesterday is the past, tomorrow is the future. Today is a gift, that is why we call it the present.
Yes. But you can only do that if you don''t use the embedded class in-size in the outer class. i.e., if you use only pointers and references to SomethingElse, you''re fine.
If you need an object of SomethingElse in your Something class, then you have to put the implmentation in Something:
When you define Foo in your CPP file, you do it like this:
void Something::SomethingElse::Foo () { ... }
If you need an object of SomethingElse in your Something class, then you have to put the implmentation in Something:
|
When you define Foo in your CPP file, you do it like this:
void Something::SomethingElse::Foo () { ... }
I think the answer is actually no. Instead...
If you derived SomethingElse from Something and then used an instance of SomethingElse in Something you would create a circular referance and it wouldn't compile. The reason is that SomethingElse would be instantiated when you instantiated Something. Since SomethingElse was derived from Something that would mean an instance of Something would be instantiated which starts the cycle all over again. The reverse is no problem. A derived class can contain an instance of it's base class.
You run into a similar problem if you try to instantiate an instance of a class within the constructor of the class. That will compile, but is an infinite loop unless you provide logic to stop it from calling itself at some point. That can be done by either calling differant constructors or passing a parameter to the constructor that determines whether it will attempt to instantiate an instance of itself. A copy constructor for a binary tree might do that. You create a node off of another tree which then creates a left and right node off the left and right subtrees. The contents of the tree being copied then explicitly stop the process, i.e. if there is no subtree then don't construct a subnode.
As for defining SomethingElse within Something that is mainly an issue of what scope you want the name SomethingElse to have. It has no real impact beyond the rules of scoping. Personally I just use namespaces for those issues, but it is a matter of preferance.
Edited by - LilBudyWizer on March 12, 2001 7:52:48 PM
|
If you derived SomethingElse from Something and then used an instance of SomethingElse in Something you would create a circular referance and it wouldn't compile. The reason is that SomethingElse would be instantiated when you instantiated Something. Since SomethingElse was derived from Something that would mean an instance of Something would be instantiated which starts the cycle all over again. The reverse is no problem. A derived class can contain an instance of it's base class.
You run into a similar problem if you try to instantiate an instance of a class within the constructor of the class. That will compile, but is an infinite loop unless you provide logic to stop it from calling itself at some point. That can be done by either calling differant constructors or passing a parameter to the constructor that determines whether it will attempt to instantiate an instance of itself. A copy constructor for a binary tree might do that. You create a node off of another tree which then creates a left and right node off the left and right subtrees. The contents of the tree being copied then explicitly stop the process, i.e. if there is no subtree then don't construct a subnode.
As for defining SomethingElse within Something that is mainly an issue of what scope you want the name SomethingElse to have. It has no real impact beyond the rules of scoping. Personally I just use namespaces for those issues, but it is a matter of preferance.
Edited by - LilBudyWizer on March 12, 2001 7:52:48 PM
Keys to success: Ability, ambition and opportunity.
Sure you can, what Stoffel has there works fine.
I often define private structs for stuff I need in the implementation - and use overloaded constructors to do various initializations...
Magmai Kai Holmlor
- The disgruntled & disillusioned
I often define private structs for stuff I need in the implementation - and use overloaded constructors to do various initializations...
Magmai Kai Holmlor
- The disgruntled & disillusioned
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement