How to forward declare classes within namespace?

Discussion in 'C++' started by Divick, Nov 7, 2005.

  1. Divick

    Divick Guest

    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
     
    Divick, Nov 7, 2005
    #1
    1. Advertising

  2. On 2005-11-07 10:40:32 -0500, "Divick" <> 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
     
    Clark S. Cox III, Nov 7, 2005
    #2
    1. Advertising

  3. 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
     
    Victor Bazarov, Nov 7, 2005
    #3
  4. 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
     
    =?ISO-8859-15?Q?Juli=E1n?= Albo, Nov 7, 2005
    #4
  5. "Divick" <> wrote in message
    news:...
    > 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.
     
    Louis Newstrom, Nov 7, 2005
    #5
  6. Divick

    Divick Guest

    >>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
     
    Divick, Nov 8, 2005
    #6
  7. Divick

    Divick Guest

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

    Thanks,
    Divick
     
    Divick, Nov 8, 2005
    #7
  8. Divick

    Divick Guest

    >>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
     
    Divick, Nov 8, 2005
    #8
  9. "Divick" <> wrote in message
    news:...

    > 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.
     
    Louis Newstrom, Nov 8, 2005
    #9
  10. Divick

    Divick Guest

    >>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
     
    Divick, Nov 8, 2005
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. amit gulati

    forward declare a template class

    amit gulati, Nov 17, 2003, in forum: C++
    Replies:
    1
    Views:
    488
    Victor Bazarov
    Nov 17, 2003
  2. Binary

    typedef forward declare

    Binary, Dec 5, 2006, in forum: C Programming
    Replies:
    20
    Views:
    899
    santosh
    Jan 27, 2007
  3. Replies:
    4
    Views:
    3,865
    Neil Cerutti
    Nov 29, 2005
  4. Replies:
    23
    Views:
    2,896
    Shezan Baig
    Jan 31, 2006
  5. Replies:
    1
    Views:
    1,417
    Jim Langston
    Nov 9, 2006
Loading...

Share This Page