Question about using namespace std

Discussion in 'C++' started by Schizoid Man, Aug 22, 2006.

  1. Schizoid Man

    Schizoid Man Guest

    Hi,

    I'm a novice whose just about migrating to C++ after cutting my teeth on
    C for a few years.

    To start with I'm using Microsoft's out-of-the-box Visual C++ Express
    Edition compiler and I had a couple of questions:

    1. If I am using namespace std, is there a specific reason for me to
    place std:: before all the manipulators - cout, endl, etc? I've noticed
    that without the std:: prefix, all the methods work as expected.

    2. I've been skimming Deitel's How to Program in C++ and noticed
    something very strange. In the chapter on functions, the author's code
    contains the function method AFTER the main() method, even though the
    main() method calls the function. This would throw a compile error in C,
    so is this valid syntax in C++?

    3. Another question about bridging C and C++: I do know that an implicit
    conversion from void* to double* is completely legal in C, but (so I'm
    told) illegal in C++. Is there a reason for the latter?

    Thanks in advance,
    Schiz
     
    Schizoid Man, Aug 22, 2006
    #1
    1. Advertising

  2. * Schizoid Man:
    >
    > I'm a novice whose just about migrating to C++ after cutting my teeth on
    > C for a few years.
    >
    > To start with I'm using Microsoft's out-of-the-box Visual C++ Express
    > Edition compiler and I had a couple of questions:
    >
    > 1. If I am using namespace std, is there a specific reason for me to
    > place std:: before all the manipulators - cout, endl, etc? I've noticed
    > that without the std:: prefix, all the methods work as expected.


    Namespace qualification (writing "std::") is required unless you have a
    "using" declaration or directive.

    See the FAQ item "Should I use using namespace std in my code?"
    currently at <url:
    http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.5>.


    > 2. I've been skimming Deitel's How to Program in C++ and noticed
    > something very strange. In the chapter on functions, the author's code
    > contains the function method AFTER the main() method, even though the
    > main() method calls the function. This would throw a compile error in C,
    > so is this valid syntax in C++?


    AFAIK in C you /can/ use an undeclared function, and that's part of the
    reason why it's not a good idea to cast the result of malloc in C. In
    C++ you /can't/ use a so far undeclared free-standing function. So if
    you're talking about free-standing functions, then the assertion in your
    last sentence has it exactly backwards.

    However, it seems you're talking about a class with a member function
    'main'. Generally a class that "does" is a Bad Idea (TM), except for
    functor and functoid classes. See the FAQ item "] What the heck is a
    functionoid, and why would I use one?" currently at <url:
    http://www.parashift.com/c++-faq-lite/pointers-to-members.html#faq-33.10>.
    Another exception: sometimes a class includes a "doer" function or
    functions for purposes of automated unit testing. But in general, steer
    away from thinking of a class as "doing" something: that's a function.

    Anyway, when you write a class with inline function bodies like

    struct S
    {
    void foo() { bar(); }
    void bar() { say( "Heh" ); }
    };

    it's as if you wrote (it's a shorthand notation for)

    struct S
    {
    void foo();
    void bar();
    };

    inline void S::foo() { bar(); }

    inline void S::bar() { say( "Heh" ); }

    and here you can see that S::foo doesn't actually call a function S::bar
    that hasn't yet been declared: it's just the shorthand notation for
    defining inline member functions, where you place the function body in
    the class definition, that makes it seem that way.


    > 3. Another question about bridging C and C++: I do know that an implicit
    > conversion from void* to double* is completely legal in C, but (so I'm
    > told) illegal in C++. Is there a reason for the latter?


    C++ is more type-oriented than C, attempting to have somewhat stronger
    static type checking.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Aug 22, 2006
    #2
    1. Advertising

  3. Schizoid Man

    Mark P Guest

    Schizoid Man wrote:
    > Hi,
    >
    > 2. I've been skimming Deitel's How to Program in C++ and noticed
    > something very strange. In the chapter on functions, the author's code
    > contains the function method AFTER the main() method, even though the
    > main() method calls the function. This would throw a compile error in C,
    > so is this valid syntax in C++?
    >


    I haven't seen the book, but perhaps the author has declared the
    functions called in main somewhere earlier. From the sounds of it, I
    would guess that they're declared in an included header file.

    This is legal:



    // foo.h

    void foo(); // no definition, just a declaration




    // foo.cpp

    #include "foo.h"

    int main ()
    {
    foo();
    }

    void foo()
    {
    // do stuff
    }
     
    Mark P, Aug 22, 2006
    #3
  4. Schizoid Man

    Jim Langston Guest

    "Schizoid Man" <> wrote in message
    news:ecfqng$ifc$...
    > Hi,
    >
    > I'm a novice whose just about migrating to C++ after cutting my teeth on C
    > for a few years.
    >
    > To start with I'm using Microsoft's out-of-the-box Visual C++ Express
    > Edition compiler and I had a couple of questions:
    >
    > 1. If I am using namespace std, is there a specific reason for me to place
    > std:: before all the manipulators - cout, endl, etc? I've noticed that
    > without the std:: prefix, all the methods work as expected.


    No, there is no need to use std:: before manipulators if you are using
    namespace std. But you can, as you have found, use them anyway.

    It is my personal opionion, and many others, never to use using namespace
    std as it brings everything into the unnamed namespace, defeating the
    purpose of namespaces in the first place. A better way is not to use using
    at all but prefix with std:: always, or you can bring in the specific things
    you want.

    using std::cout;
    using std::endl;
    etc..

    > 2. I've been skimming Deitel's How to Program in C++ and noticed something
    > very strange. In the chapter on functions, the author's code contains the
    > function method AFTER the main() method, even though the main() method
    > calls the function. This would throw a compile error in C, so is this
    > valid syntax in C++?


    I'm guessing this is for methods in a class?

    > 3. Another question about bridging C and C++: I do know that an implicit
    > conversion from void* to double* is completely legal in C, but (so I'm
    > told) illegal in C++. Is there a reason for the latter?


    C++ is much more strict on types than C. Makes it better so that we don't
    make as many stupid mistakes.
     
    Jim Langston, Aug 24, 2006
    #4
    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. Petter Reinholdtsen
    Replies:
    9
    Views:
    4,398
    Howard
    Nov 29, 2004
  2. Replies:
    7
    Views:
    438
    Jacek Dziedzic
    Oct 3, 2006
  3. Jeffrey Walton
    Replies:
    10
    Views:
    956
    Mathias Gaunard
    Nov 26, 2006
  4. bb
    Replies:
    1
    Views:
    313
    Alf P. Steinbach
    Sep 7, 2007
  5. Shriramana Sharma
    Replies:
    2
    Views:
    152
    Shriramana Sharma
    Jun 27, 2013
Loading...

Share This Page