How to forward declare classes within namespace?

D

Divick

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
 
C

Clark S. Cox III

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?
 
V

Victor Bazarov

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
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

Divick said:
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,
 
L

Louis Newstrom

Divick said:
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.
 
D

Divick

Java packages are used to access code in another directory. So if a Java
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++.
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
 
D

Divick

Well I really meant was there could possibly be some other syntax to
distinguish that.

Thanks,
Divick
 
D

Divick

Java packages are used to access code in another directory. So if a Java
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++.
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
 
L

Louis Newstrom

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?
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.
 
D

Divick

How would someone #import a class with no package? Specifically, how do youYou 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.
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.)
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top