# Unity predefining a class in the namespace of class

This topic is 3990 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello Dear community I want to define a class in the namespace of another class, like this:
class A
{
public:
class B
{
...
};
B Something;
...
};

But I want to define A and B in sperate files. Since A uses B for a member-variable (like "Something" in the example), B must be defined befor A. So I tried: A.h:
include <B.h>

class A
{
public:
B Something;
...
};

B.h:
class A;

class A::B
{
...
}

Does not work, I get "qualified name does not name a class" at the definition of B. How is it done correctly? Thansk! Nathan

##### Share on other sites
You can't. C++ does not allow you to declare a single member of a class, you have to declare them all simultaneously. However, you don't have to make B a subclass of A: a typedef is enough.

// B.hppclass B{};// A.hpp#include "B.hpp"class A{public:  typedef ::B B;  A::B Something;};

##### Share on other sites
Quote:
 Original post by ToohrVykYou can't. C++ does not allow you to declare a single member of a class, you have to declare them all simultaneously. However, you don't have to make B a subclass of A: a typedef is enough.*** Source Snippet Removed ***

But what would the benifits of that be?

Class scope definitions of other classes are generally used to reduce the scope and give meaning to the second class, whereas the code you suggest not only leaves class B in the global namespace, but actually gives you multiple ways of defining the same object (since you have to include b.hpp to use a.hpp).

If class B is a generic class that would be used by only a select few classes, then the inclusion and scope of B should be altered, rather than adding additional ways of referencing it.

Spree

##### Share on other sites
Quote:
 Original post by SpreeTreeBut what would the benifits of that be?

Being able to move the definition of B to its own file and out of A.

Quote:
 Class scope definitions of other classes are generally used to reduce the scope and give meaning to the second class, whereas the code you suggest not only leaves class B in the global namespace, but actually gives you multiple ways of defining the same object (since you have to include b.hpp to use a.hpp).

_Vector_const_iterator is a template class in the std:: namespace, but nobody ever uses it. In fact, few people even suspect its existence, and it isn't guaranteed to exist on all compilers or libraries either. Most people use std::vector<T>::const_iterator instead, even if it's just a typedef for std::_Vector_const_iterator<T>.

The fact that a class is in the global namespace (or any namespace, for that matter) doesn't mean that it's public—besides, naming it _private_impl_A::_B (with the appropriate name and namespace) is extremely easy to do, and conveys the "don't touch this class" message pretty clearly to programmers. Users should not be aware of the existence of _private_impl_A::_B and the "B.hpp" header.

Ultimately, it is the documentation of the signature of the module, and not the header files, that should be used by the programmers.

##### Share on other sites
Quote:
Original post by ToohrVyk
Quote:
 Original post by SpreeTreeBut what would the benifits of that be?

Being able to move the definition of B to its own file and out of A.

I meant besides that ;)

Quote:
 Original post by ToohrVykThe fact that a class is in the global namespace (or any namespace, for that matter) doesn't mean that it's public—besides, naming it _private_impl_A::_B (with the appropriate name and namespace) is extremely easy to do, and conveys the "don't touch this class" message pretty clearly to programmers. Users should not be aware of the existence of _private_impl_A::_B and the "B.hpp" header.Ultimately, it is the documentation of the signature of the module, and not the header files, that should be used by the programmers.

I understand your reasoning behind that (and the private impl namespace is a good idea), but unfortunatly to a lot of people the fact that it is global would directly lead them to think it was public.

With the advent of Visual Assist and (to a lesser extent) itellisense showing you everything and more within your current space, people would know of it's existence whether you wanted them to or not.

I have also worked with lot of professional libraries that have little or no documentation (of course you could question the term professional in that case) which is why I try to go to great lengths to show the use and scope of a class directly from the source, rather than only relying on the documentation :)

This is a useful tool in the big box of programmer tools (and there are plenty of ways around limiting the use of B within the space of program), I just thought I would mention a problem some people would have with your solution.

Spree

##### Share on other sites
I want B to be declared in A because it is only used as a member of A and should not be visible from the outside.
On the other hand its big enough to make the class decleration of A look complicated.
For that reason ToohrVyks Idea seems not so good here.

What would you do in this case?
Nathan

##### Share on other sites
You also have the possibility of:

// A.hppclass A{  // ...public:#define A_INCLUDING_B#  include "_B.hpp"#undef A_INCLUDING_B  B Something;};// _B.hpp#ifndef A_INCLUDING_B#  error This header file should not be included#endifclass B{  // ...};

• 16
• 9
• 13
• 41
• 15
• ### Similar Content

• By Aryndon
Project Redemption is an semi-fantasy RPG with a linear story and an elaborate combat system.
We are building in Unity and are currently looking animators and artists.
What we are looking for
-Someone who is okay with split revenue/profits when finished
-Collaborate with others in the team. Do you have an idea/thought on what should be included? Tell us!
If you are interested. Please message me and I will get back to you as soon as possible! Or add me on Discord AJ#6664
• By Aggrojag
Hello!
I'm working on a game that is a narrative driven dark comedy with some small aspects of platforming and puzzle solving. The project is rather small as well. It touches on topics such as suicide, mental illness, family, corruption, free-will, and redemption.
This project is exercise in polish, this means some experimentation will be needed along with some reworking of assets as they're provided.
This will be a revshare model.
First, I'm looking for a 2D sprite artist, not pixelated, that can compliment the style of the attached images, and be similar to the temporary character.
We are looking to bring on a SFX designer at this time. Full list of required SFX will be available upon request, as well as a build with all elements related to sound implemented in some form (many SFXs pulled from the web for now). Will likely require some field recording, and some pretty strange SFX for when things get weird. I imagine a good portion of these will be quite fun to create.
Lastly, I'm looking for a male voice actor, English should be your primary language. There will be at minimum two characters that will need to be brought to life through vocals. The first voice is similar to Marvin from Hitchhiker's Guide to the Galaxy. A reference for the second voice would be a mix of Ren (Ren & Stimpy), and Android 21 (DragonBallFighterZ). Due to formatting, I'm not including YouTube links in the post, sorry.

WIP Scene with our custom shaders attached (platforms are lazily placed, as this was an asset test):

A scene with dynamic lighting and temp character:

If you made it to the bottom, thank you, and I look forward to hearing from you.

• Ok, firstly, Hi.

This is my first post on this forum. I am an Indie Dev making my first game so bear with me when I say dumb stuff, I'm on a huge learning curve.

My first question is about inventory systems for unity. I am trying to make a survival type game with crafting. I have purchased Inventory manager pro by devdog from the unity asset store and it seems like a pretty powerful assett but for an intermediate coder its a little tough to use.  I'm beginning to wonder if it was the right purchase.
So my question is.... does anyone have any experience of inventory plugins / systems for unity and can anyone reccomend a system to me?
It needs to have the following: Loot system, crafting system, character sheet, blueprint system,  character stats system. Ideally with as little coding as possible.

Thanks

• I've got a bug with my brick breaker style game. The bricks move down one line at a time ever 1.5 seconds. What appears to be happening is occasionally the ball will be just about to hit the brick when the brick moves down a line, and now the ball is behind it. I'm not sure how to fix this. I have two ideas but I'm not sure of implementation. 1 solution would be to check where they were and where they are going to be before rendering the frame. Then if they crossed paths, then register the brick as hit. Solution 2 would be change how the bricks move. I could maybe slide them down line by line, instead of a jump down. I'm not sure of this will fix the issue or not. Any ideas?
• By Pixeye
I wrote an extension for unity inspector that allows to group/fold variables.
Available on github  , cheers!