473,782 Members | 2,554 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

namespace for a class scope

Hi,
Is it possible to have a class level namespace opening instead of
file level .

Something like this,
I have a long namespace like
namespace com { namespace my_company{ namespace my_project{ namespace
parser{
class my_parser{
};
}}}}

Now when I want to use this , I usually make a shortcut and use it,
with the assumption that I am working at my_project level, thus no
name resolution at that level is needed.

thus in the file,
namespace parser = com::my_company ::my_project::p arser;

Thus it is in the file scope.
Then use it as parser::my_pars er in the header;
The same thing also can be done at function scope.

However, how this can be done in the class scope ?
like,

class my_parser_usage {
namespace parser = com::my_company ::my_project::p arser;
public:
parser::my_pars er get_pasrer()con st;
}
class my_second_parse r_usage{
namespace parser = com::my_company ::my_project::p arser2;
public:
parser::my_pars er get_pasrer()con st;
}

Thanks
abir

Feb 9 '07 #1
3 1821
toton wrote:
Hi,
Is it possible to have a class level namespace opening instead of
file level .

Something like this,
I have a long namespace like
namespace com { namespace my_company{ namespace my_project{ namespace
parser{
class my_parser{
};
}}}}

Now when I want to use this , I usually make a shortcut and use it,
with the assumption that I am working at my_project level, thus no
name resolution at that level is needed.

thus in the file,
namespace parser = com::my_company ::my_project::p arser;

Thus it is in the file scope.
Then use it as parser::my_pars er in the header;
The same thing also can be done at function scope.

However, how this can be done in the class scope ?
like,

class my_parser_usage {
namespace parser = com::my_company ::my_project::p arser;
public:
parser::my_pars er get_pasrer()con st;
}
class my_second_parse r_usage{
namespace parser = com::my_company ::my_project::p arser2;
public:
parser::my_pars er get_pasrer()con st;
}

Thanks
abir
As best I can tell from the grammar, a "member-specification" can't
contain a "namespace-alias-definition". That is, you can't use
namespace aliases in a class.

An alternative might by a typedef:
class my_parser_usage {
typedef com::my_company ::my_project::p arser::my_parse r my_parser;
public:
my_parser get_parser() const;
};

Though it doesn't achieve quite the same effect you were going for.

--
Alan Johnson
Feb 9 '07 #2
toton wrote:
>
However, how this can be done in the class scope ?
like,

class my_parser_usage {
namespace parser = com::my_company ::my_project::p arser;
public:
parser::my_pars er get_pasrer()con st;
}
class my_second_parse r_usage{
namespace parser = com::my_company ::my_project::p arser2;
public:
parser::my_pars er get_pasrer()con st;
}
I am using dummy namespaces in the case

namespace X{
namespace parser = com::my_company ::my_project::p arser;

class my_parser_usage
{
public:
parser::my_pars er get_pasrer()con st;
};}
using X::my_parser_us age;

--
Maksim A. Polyanin

"In thi world of fairy tales rolls are liked olso"
/Gnume/
Feb 10 '07 #3
On Feb 9, 5:42 pm, "toton" <abirba...@gmai l.comwrote:
Hi,
Is it possible to have a class level namespace opening instead of
file level .

Something like this,
I have a long namespace like
namespace com { namespace my_company{ namespace my_project{ namespace
parser{
class my_parser{
};

}}}}

Now when I want to use this , I usually make a shortcut and use it,
with the assumption that I am working at my_project level, thus no
name resolution at that level is needed.

thus in the file,
namespace parser = com::my_company ::my_project::p arser;

Thus it is in the file scope.
Then use it as parser::my_pars er in the header;
Putting your namespace alias in the header would normally be something
to avoid. By putting it in a header, you are making it visible to
anyone who includes that header. Unless this is intentional and that
clients using the header expect this behavior, it would normally be
considered bad practice, bad design or both. Having said that, your
own situation may make this a perfectly valid choice, so take my
comments with a grain of salt if you like. ;)

A more dangerous habit would be to put the following in a header:

// maybe append ::parser to the following, but see below
using namespace com::my_company ::my_project;

The above would bring my_project's contents into scope, which includes
parser, but also everything else in my_project. You could
append ::parser to the above using statement, but then there would be
no point in having the parser namespace at all. The reason I mention
this is to contrast it to what a namespace alias does for you. The
using statement makes the parser namespace redundant, whereas your
namespace alias makes parser's contents available via more than one
"scope path". This is not an error, but it does create ambiguity in
your interface. Again, if this is intentional, then fine, but normally
you wouldn't want to intentionally create such ambiguity when you have
a choice. In your own .cpp files, go nuts and use the namespace alias
as you want (that's what it is there for), but in header files, you
should give serious thought to whether you really want to do that to
your interface.

--
Computational Fluid Dynamics, CSIRO (CMIS)
Melbourne, Australia

Feb 10 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
6479
by: Anonymous | last post by:
Is namespace the same thing as scope? While reading the book "Thinking in C++", I was under the impression that namespace is, well, a namespace--a feature to create a hiearchy for identifiers within the Global Namespace. And that, all identifiers within a given namespace, however deeply nested they are, each of them has the Global Scope. So, if namespace outer { int a = 1;
1
1865
by: JustSomeGuy | last post by:
I am writing classes and I want them to belong to mynamespace What is the syntax to say that the class I'm defining is a member of mynamespace? What is the scope of the syntax and how does one go back to the previous namespace definition? TIA..
3
1256
by: CHRISTOF WARLICH | last post by:
Hi, the following few lines of code are showing a quite strange and unexpected behaviour of namespaces that makes me worry wheater I should rely on namespaces in the future at all. The example below compiles if OK is defined, but gives the following error otherwise:
2
7515
by: Tony Johansson | last post by:
Hello! I'm reading a book about C++ and there is something that I don't understand so I ask you. Below I have the text from the book and the code from the file where main is located and some namespace definition with class definitions. The book says "C++ has a global anonymous namespace that is similar to Java's global anonymous package. All declarations not explicitly placed in named
9
2452
by: Steven T. Hatton | last post by:
It was once suggested to me that I could accomplish much the same thing that modules would accomplish (if C++ had modules) by writing my entire program - except for main() - inside of a class. When it was suggested, I didn't take it very seriously, but I have recently begun wondering if it is an idea worth considering. I'm now trying to think of what fundamental differences might exist between namespace scope, and class scope. One that...
4
3083
by: Kevin Newman | last post by:
The primary problem I've had with php is the lack of namespaces, which makes OOP very difficult to organize, since you end up with large number of classes cluttering up the same namespace - which leads to a secondary problem involving php's __autoload feature. Since you cannot specify a namespace when calling a class that may not have been included, you are forced to store all of your classes in the same folder in your file system. This...
1
1525
by: toton | last post by:
Hi, Is it possible to have a class level namespace opening instead of file level . Something like this, I have a long namespace like namespace com { namespace my_company{ namespace my_project{ namespace parser{ class my_parser{ };
7
2077
by: Juha Nieminen | last post by:
This is possible: namespace X { class A; } class X::A { <implementation}; However, what about nameless namespaces? Does this do what I want? namespace { class A; }
3
1859
by: mackenzie | last post by:
I was wondering why it is "ill-formed if an allocation function is declared in a namespace"? I have done some searching on the web and can not find a reason why; I have stumbled across a few other users asking similar questions but there are no definitive replies that I could find. To me a namespace specifies a set of functionality. From an application point of view a namespace could easily specify a set of interrelated processes and...
0
10308
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10143
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10076
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9939
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8964
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6729
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5507
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4040
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3633
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.