473,396 Members | 1,892 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,396 software developers and data experts.

Question about using namespace std

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
Aug 22 '06 #1
3 2195
* 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?
Aug 22 '06 #2
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
}
Aug 22 '06 #3

"Schizoid Man" <sc***@sf.comwrote in message
news:ec**********@geraldo.cc.utexas.edu...
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.
Aug 23 '06 #4

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

Similar topics

17
by: Medi Montaseri | last post by:
Hi, Given a collection of similar but not exact entities (or products) Toyota, Ford, Buick, etc; I am contemplating using the Abstraction pattern to provide a common interface to these products....
10
by: da Vinci | last post by:
Hello. First off, I am not sure of the exact jargon used, so I will ask a question regarding it. Then on to my other question. When you use things like cout and cin from the iostream header...
5
by: cppaddict | last post by:
It is typical to put the line: using namespace std; at the top of a file which makes use of std library objects. To take a simple example: #include <iostream> using namespace std;
6
by: clinton__bill | last post by:
Hi, I usually use "using namespace <namespace_name>" to reference a namespace. Today I run across a code, in its header file it has this, namespace SP1{ class C1; } While SP1::C1 is a...
2
by: Hazzard | last post by:
I just realized that the code I inherited is using all asp.net server controls (ie. webform controls) and when I try to update textboxes on the client side, I lose the new value of the textbox when...
11
by: Random | last post by:
I'm confused about the proper use and usefulness of namespaces. I beleive I understand the purpose is so the developer can put classes within namespaces to essentially organize your code. And I...
7
by: jason | last post by:
In the microsoft starter kit Time Tracker application, the data access layer code consist of three cs files. DataAccessHelper.cs DataAcess.cs SQLDataAccessLayer.cs DataAcccessHelper appears...
54
by: shuisheng | last post by:
Dear All, I am always confused in using constants in multiple files. For global constants, I got some clues from http://msdn.microsoft.com/en-us/library/0d45ty2d(VS.80).aspx So in header...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.