Connecting Tech Pros Worldwide Forums | Help | Site Map

How to forward declare classes within namespace?

Divick
Guest
 
Posts: n/a
#1: Nov 7 '05
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


Clark S. Cox III
Guest
 
Posts: n/a
#2: Nov 7 '05

re: How to forward declare classes within namespace?


On 2005-11-07 10:40:32 -0500, "Divick" <divick.kishore@gmail.com> said:
[color=blue]
> 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;
> }[/color]

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).
[color=blue]
>
> 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.[/color]

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
clarkcox3@gmail.com

Victor Bazarov
Guest
 
Posts: n/a
#3: Nov 7 '05

re: How to forward declare classes within namespace?


On Mon, 07 Nov 2005 07:40:32 -0800, Divick wrote:[color=blue]
> 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.[/color]

Why the hell not? That's the only way I know.
[color=blue]
>
> namespace myVeryOwnNamespace
> {
> class myClass1;
> }
>
> class X
> {
> public:
> myClass1 * _mClass;[/color]

That shouldn't compile. 'myCLass1' is unknown here, in ::X. The line
needs to be

myVeryOwnNamespace::myClass1 * _mClass;
[color=blue]
> };
>
> Ideally I wish if C++ allowed forward declaring classes like
> myVeryOwnNamespace::myClass1, but it seems to me this syntax is not
> allowed.[/color]

Why do you say that?
[color=blue]
> 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?[/color]

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

V
Julián Albo
Guest
 
Posts: n/a
#4: Nov 7 '05

re: How to forward declare classes within namespace?


Divick wrote:
[color=blue]
> 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.[/color]

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
Louis Newstrom
Guest
 
Posts: n/a
#5: Nov 7 '05

re: How to forward declare classes within namespace?


"Divick" <divick.kishore@gmail.com> wrote in message
news:1131378032.687815.14440@g49g2000cwa.googlegro ups.com...[color=blue]
> I find that not many people use the namespace feature of C++ even
> though it is such a strong feature.[/color]
....[color=blue]
> Does any one have explanation for this i.e. why
> at all C++ namespaces not used as extensively as Java packages?[/color]

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.


Divick
Guest
 
Posts: n/a
#6: Nov 8 '05

re: How to forward declare classes within namespace?


>>Java packages are used to access code in another directory. So if a Java[color=blue][color=green]
>>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.[/color][/color]

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++.
[color=blue][color=green]
>>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.[/color][/color]
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

Divick
Guest
 
Posts: n/a
#7: Nov 8 '05

re: How to forward declare classes within namespace?


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

Thanks,
Divick

Divick
Guest
 
Posts: n/a
#8: Nov 8 '05

re: How to forward declare classes within namespace?


>>Java packages are used to access code in another directory. So if a Java[color=blue][color=green]
>>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.[/color][/color]

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++.
[color=blue][color=green]
>>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.[/color][/color]
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

Louis Newstrom
Guest
 
Posts: n/a
#9: Nov 8 '05

re: How to forward declare classes within namespace?


"Divick" <divick.kishore@gmail.com> wrote in message
news:1131434999.016574.10910@g43g2000cwa.googlegro ups.com...
[color=blue]
> 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.[/color]

How would someone #import a class with no package? Specifically, how do you
specify the directory where that class is located?
[color=blue][color=green][color=darkred]
>>>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.[/color][/color][/color]
[color=blue]
> 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.[/color]

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.
[color=blue]
> 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.[/color]

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.


Divick
Guest
 
Posts: n/a
#10: Nov 8 '05

re: How to forward declare classes within namespace?


>>How would someone #import a class with no package? Specifically, how do you[color=blue][color=green]
>>specify the directory where that class is located?[/color][/color]
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.
[color=blue][color=green]
>>And in my experience,this rarely happens.[/color][/color]
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.)
[color=blue][color=green]
>> 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.[/color][/color]
[color=blue]
>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.[/color]

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

Closed Thread