473,385 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to forward declare classes within namespace?

Hi all,
does any one know what is the right way to forward declare
classes within namespaces. Though I have been using the syntax as
follows but it doesn't sound good to me.

namespace myVeryOwnNamespace
{
class myClass1;
}

class X
{
public:
myClass1 * _mClass;
};

Ideally I wish if C++ allowed forward declaring classes like
myVeryOwnNamespace::myClass1, but it seems to me this syntax is not
allowed.

I really wish that if C++ designers designed namespaces in much the
same way as Java handles the packages.

I find that not many people use the namespace feature of C++ even
though it is such a strong feature. If any one of you has programmed in
Java, then you would know that most of the libraries/products on this
planet take use of the feature provided by the packages. But this is
not the case with C++. Does any one have explanation for this i.e. why
at all C++ namespaces not used as extensively as Java packages?

Thanks,
Divick

Nov 7 '05 #1
9 2632
On 2005-11-07 10:40:32 -0500, "Divick" <di************@gmail.com> said:
Hi all,
does any one know what is the right way to forward declare
classes within namespaces. Though I have been using the syntax as
follows but it doesn't sound good to me.

namespace myVeryOwnNamespace
{
class myClass1;
}
What doesn't "sound good" about that? It's doing exactly what you want
(i.e. it's forward declaring the class within a namespace).

class X
{
public:
myClass1 * _mClass;
};

Ideally I wish if C++ allowed forward declaring classes like
myVeryOwnNamespace::myClass1, but it seems to me this syntax is not
allowed.


If that were allowed, how would the compiler know that it was a class
within a namespace, and not a class within a class?
--
Clark S. Cox, III
cl*******@gmail.com

Nov 7 '05 #2
On Mon, 07 Nov 2005 07:40:32 -0800, Divick wrote:
Hi all,
does any one know what is the right way to forward declare
classes within namespaces. Though I have been using the syntax as
follows but it doesn't sound good to me.
Why the hell not? That's the only way I know.

namespace myVeryOwnNamespace
{
class myClass1;
}

class X
{
public:
myClass1 * _mClass;
That shouldn't compile. 'myCLass1' is unknown here, in ::X. The line
needs to be

myVeryOwnNamespace::myClass1 * _mClass;
};

Ideally I wish if C++ allowed forward declaring classes like
myVeryOwnNamespace::myClass1, but it seems to me this syntax is not
allowed.
Why do you say that?
I really wish that if C++ designers designed namespaces in much the
same way as Java handles the packages.

I find that not many people use the namespace feature of C++ even
though it is such a strong feature. If any one of you has programmed in
Java, then you would know that most of the libraries/products on this
planet take use of the feature provided by the packages. But this is
not the case with C++. Does any one have explanation for this i.e. why
at all C++ namespaces not used as extensively as Java packages?


Huh? Aren't Java packages Java and not C++? How can C++ <whatever> be
used in a Java package?

V
Nov 7 '05 #3
Divick wrote:
follows but it doesn't sound good to me.

namespace myVeryOwnNamespace
{
class myClass1;
}

class X
{
public:
myClass1 * _mClass;
};

Ideally I wish if C++ allowed forward declaring classes like
myVeryOwnNamespace::myClass1, but it seems to me this syntax is not
allowed.


And what is the advantage? A few less characters in the source? Usually a
much more convincing argument is required to introduce a new syntax. Read
"The design and evolution of C++" if you are interested in how C++ evolved
and how to make (and how not) proposals for changes with some possibility
to be accepted,

--
Salu2
Nov 7 '05 #4
"Divick" <di************@gmail.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
I find that not many people use the namespace feature of C++ even
though it is such a strong feature. .... Does any one have explanation for this i.e. why
at all C++ namespaces not used as extensively as Java packages?


This is another example of something that LOOKS similar in C++ and Java but
really serves a different purpose.

Java packages are used to access code in another directory. So if a Java
programmer wants his class to be used by others, they pretty much have to
declare it as part of a package. That's why Java packages are used all over
the place.

C++ namespaces are used only to resolve name collisions. So in C++,
programmers don't need namespaces unless they find themselves wanting to use
two classes with the same name. This is a pretty rare situation.
Nov 7 '05 #5
>>Java packages are used to access code in another directory. So if a Java
programmer wants his class to be used by others, they pretty much have to
declare it as part of a package. That's why Java packages are used all over
the place.
That's not true. Java packages were designed not only for preventing
name clashes but also as an encapsulation mechanism. You really don't
have to provide a library in package but you can also code your
component / library in global package and still anyone can use it. Just
that there will be more probability of name clash. Since this feature
is not there all in C++ , others have designed their own technology or
methodology to achieve such things. One example of such a technology is
COM/DCOM. There was no reason to design such a technology but because
there is no such feature in C++.
C++ namespaces are used only to resolve name collisions. So in C++,
programmers don't need namespaces unless they find themselves wanting to use
two classes with the same name. This is a pretty rare situation.

Again I strongly disagree with this. Don't you feel that integrating
two totally different components , written by totally different
programmers, who don't know at all that these two components can / will
be integrated, is really painful. My experience tells that most of the
times you definitely have to modify the components., untill and unless
those components have been designed keeping in mind that they can be
used in a plug and play kind of environment.

Before anyone of you say that some syntax /logic for the design in some
language is not right , you must have some really good programming
experience with that language. I have been programming with various
languages like C# and Java precisely. There are certain features of
these languages which are really strong which I feel can also be
incorporated into C++. Though I agree both of these languages are sort
of conglomerate of best features of ADA/C++ and other object oriented
languages, but still they have been designed very elegantly. Its a
breeze programming with these languages. If these languages can adapt
themselves with the times, then why the hell C++ cannot adopt some
novel features / syntax of these languages. Have we really closed our
eyes and ears for any such criticism / rationalism ?

Any comments / criticism is welcome.
Anyways thanks for you reply,
Divick

Nov 8 '05 #6
Well I really meant was there could possibly be some other syntax to
distinguish that.

Thanks,
Divick

Nov 8 '05 #7
>>Java packages are used to access code in another directory. So if a Java
programmer wants his class to be used by others, they pretty much have to
declare it as part of a package. That's why Java packages are used all over
the place.
That's not true. Java packages were designed not only for preventing
name clashes but also as an encapsulation mechanism. You really don't
have to provide a library in package but you can also code your
component / library in global package and still anyone can use it. Just
that there will be more probability of name clash. Since this feature
is not there all in C++ , others have designed their own technology or
methodology to achieve such things. One example of such a technology is
COM/DCOM. There was no reason to design such a technology but because
there is no such feature in C++.
C++ namespaces are used only to resolve name collisions. So in C++,
programmers don't need namespaces unless they find themselves wanting to use
two classes with the same name. This is a pretty rare situation.

Again I strongly disagree with this. Don't you feel that integrating
two totally different components , written by totally different
programmers, who don't know at all that these two components can / will
be integrated, is really painful. My experience tells that most of the
times you definitely have to modify the components., untill and unless
those components have been designed keeping in mind that they can be
used in a plug and play kind of environment.

Before anyone of you say that some syntax /logic for the design in some
language is not right , you must have some really good programming
experience with that language. I have been programming with various
languages like C# and Java precisely. There are certain features of
these languages which are really strong which I feel can also be
incorporated into C++. Though I agree both of these languages are sort
of conglomerate of best features of ADA/C++ and other object oriented
languages, but still they have been designed very elegantly. Its a
breeze programming with these languages. If these languages can adapt
themselves with the times, then why the hell C++ cannot adopt some
novel features / syntax of these languages. Have we really closed our
eyes and ears for any such criticism / rationalism ?

Any comments / criticism is welcome.
Anyways thanks for you reply,
Divick

Nov 8 '05 #8
"Divick" <di************@gmail.com> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.com...
You really don't
have to provide a library in package but you can also code your
component / library in global package and still anyone can use it.
How would someone #import a class with no package? Specifically, how do you
specify the directory where that class is located?
C++ namespaces are used only to resolve name collisions. So in C++,
programmers don't need namespaces unless they find themselves wanting to
use
two classes with the same name. This is a pretty rare situation.

Again I strongly disagree with this. Don't you feel that integrating
two totally different components , written by totally different
programmers, who don't know at all that these two components can / will
be integrated, is really painful.
Yes. That would be very painful. But I'm not sure what you are disagreeing
with. Namespaces are the way to solve name collisions. You don't have to
use namespaces if you don't have such a collision. And in my experience,
this rarely happens.
Before anyone of you say that some syntax /logic for the design in some
language is not right , you must have some really good programming
experience with that language.


I am not saying that it is not right. I was merely answering why C++
namespaces are not used as extensively as Java packages. The answer was
simply that they have different uses. I said nothing about one being better
or one being not right.
Nov 8 '05 #9
>>How would someone #import a class with no package? Specifically, how do you
specify the directory where that class is located? You can simply place the files in the same directory as your
development classes and then to import you just have to specify the
class name without the directory name. or otherwise you can set your
classpath. Also one can have his classes packaged as jar file as well
without directories.
And in my experience,this rarely happens. I was disagreeing with that one specially. It is pretty common to give
common names to the classes and hence the problem doesn't arise because
not everyone does integration kind of stuff with C++ that frequently
(the reason for that is because that is pretty hard to do and hence
fewer people.)
Before anyone of you say that some syntax /logic for the design in some
language is not right , you must have some really good programming
experience with that language.
I am not saying that it is not right. I was merely answering why C++
namespaces are not used as extensively as Java packages. The answer was
simply that they have different uses. I said nothing about one being better
or one being not right.


And as far as this goes, well I was not pointing to you. I think I
stated it wrongly. What I really meant was that there are certain
features of certain languages which are really nice and which have
evolved over time. And in my opinion people should try to incoporate
those features in C++ without much affecting the coding style/scheme/
backward compatibility.
This was intended for others mails in this thread.

Thanks,
Divick

Nov 8 '05 #10

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

Similar topics

1
by: amit gulati | last post by:
Is it possible to forward declare a template class using "class XXX". If yes, then what is the syntax??
4
by: John Alway | last post by:
Hello, Is it possible to instantiate and use a c++ class from within a DLL? I ask this, because I attempted to create a class within a DLL, and export its methods via wrapper C functions, and...
2
by: jw56578 | last post by:
Hi, Is there a way to discover all the classes within a given namespace? thanks
4
by: yuliy | last post by:
Hello gurus, I stuck in following: how can I do forward declaration if the forward declared class is in some namespace? something like // header class std::string; // approach#1
23
by: mark.moore | last post by:
I know this has been asked before, but I just can't find the answer in the sea of hits... How do you forward declare a class that is *not* paramaterized, but is based on a template class? ...
1
by: ctor | last post by:
Hi, I'm experimenting with using a lot of different namespaces in my current project to see if it helps me keep my code more organized. In some ways I'm finding that it causes more problems...
1
by: toton | last post by:
Hi, I have two namespace contains class InkFrame and PrefDialog respectively. PrefDialog needs InkFrame to show the dialog over the frame. It also stores a pointer to InkFrame inside it. Now I...
20
by: Binary | last post by:
Hi, With keyword struct, we can simply do forward declare by: struct struct_a; but if we typedef it: typdef struct struct_a struct_a_t;
6
by: matrem | last post by:
I wish to make a method of a base class a friend of a method of a derived class. However, this makes me quickly run into a circular dependency problem. I must implement the base class first, but it...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.