The ProgrammersTalk Community
Forum Register Search Today's Posts Mark Forums Read
Register

Go Back   The ProgrammersTalk Community > General Programming > C / C++


Welcome to the The ProgrammersTalk Community forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.
Reply
 
LinkBack Thread Tools    Display Modes   
  #11 (permalink)  
Old 07-26-2007, 03:39 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
PT Admin
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,118
iTrader: (0)
HelloWorld is a jewel in the roughHelloWorld is a jewel in the roughHelloWorld is a jewel in the rough
Quote:
you can use global variables to affect your entire file
So for example if I declare:

Code:
using std::cin;
in test.cpp
I don't have to declare it again in any of my other files within that solution or folder (where test.cpp is at..)? Or how does it exactly works?

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #12 (permalink)  
Old 07-26-2007, 04:02 PM
Bench Bench is offline
Full Programmer
Join Date: Jul 2007
Location: UK
Posts: 113
iTrader: (0)
Bench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished road
Quote:
Originally Posted by HelloWorld View Post
So for example if I declare:

Code:
using std::cin;
in test.cpp
I don't have to declare it again in any of my other files within that solution or folder (where test.cpp is at..)? Or how does it exactly works?
No, only in that one file. You could potentially get name clashes in test.cpp if you created a variable or function called 'cin', because the name would already be reserved. It wouldn't necessarily have any effect on any other files. (Although it can, if you're doing it in a .h header file) The main reason for namespaces is to stop name clashes from happening. One thing that 'using' does is un-do the name hiding which namespaces provide.

Essentially, its a tool for laziness. since you can still access 'cin' regardless, by prefixing the name with std::

here's a program with a 'using' directive (the lazy way)
Code:
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string my_str = "Hello, World";
    cout << my_str << endl;
}
And here's one without (the un-lazy way)
Code:
#include <iostream>
#include <string>

int main()
{
    std::string my_str = "Hello, World";
    std::cout << my_str << std::endl;
}

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!

Last edited by Bench : 07-26-2007 at 04:21 PM.
Reply With Quote
The Following User Says Thank You to Bench For This Useful Post:
HelloWorld (07-26-2007)
  #13 (permalink)  
Old 07-26-2007, 04:42 PM
rpgfan3233 rpgfan3233 is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jul 2007
Posts: 118
iTrader: (0)
rpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura about
using is similar to import in that you need to declare it in every file you want to use it in. For example, in Java you can use import java.io.*; in a MsgSend class. Then in a MsgRecv class, you would need to use import java.io.*; again, right? The same is true in C++. If you have a file test.cpp, using std::cin; is only valid for test.cpp. In other words, another file such as main.cpp would have no knowledge of that using declaration. It is all about the scope of various items. If you used using std::cin; inside main(), you would only be able to use cin in main(). If you tried to use cin in another function, you would receive an compile error. You would either need to use using std::cin; before actually using cin inside the function or you would need to use the fully-qualified name std::cin for every time that you used cin. Example:
Code:
#include <iostream>

// Answer To Life, the Universe and Everything test
bool answerCorrect (int answer) {
    if (answer != 42)
        return false;
    return true;
}

// asks user to decide to try again or not
int tryAgain () {
    int answer = 'q';
    std::cout << "If you wish to quit, type 'q' and press Enter." << std::endl;
    std::cout << "Otherwise, type another character and press Enter. ";
    answer = std::cin.get();
    if (!std::cin.good())
        std::cin.clear();
    std::cin.sync();
    return answer;
}

int main () {
    using std::cin;
    using std::cout;
    using std::cerr;
    using std::endl;

    int ans = 0;
    do {
        cout << "What is the answer to Life, the Universe and Everything? ";
        cin >> ans;
        if (!cin.good()) { //invalid input such as 'a' instead of a number
            cin.clear();
            cerr << "Your answer was invalid." << endl << endl;
        }
        else if (!answerCorrect(ans))
            cout << "Sorry, but your answer was incorrect." << endl << endl;
        else {
            cout << "Correct!" << endl;
            break; //exit the loop so the program can end
        }
        cin.sync(); //discard leftover characters in the input buffer
    } while (tryAgain() != 'q');
    return 0;
}
Simple code, but effective. I apologize for the formatting when the input is invalid or someone wants to play again. However, it was just something quick to illustrate the purpose.

Wherever you put using, whether it is globally (outside of everything) or within a function such as main() or whatever, it is confined to that file and further confined to the block of code it is enclosed by, if it is not global.

__________________
"C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off."
-- Bjarne Stroustrup, creator of what is now known as C++
For more quotes by Bjarne Stroustrup, check out http://www.research.att.com/~bs/bs_faq.html#really-say-that.
Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!

Last edited by rpgfan3233 : 07-26-2007 at 04:44 PM. Reason: A quick addendum to finish the explanation.
Reply With Quote
The Following User Says Thank You to rpgfan3233 For This Useful Post:
HelloWorld (07-26-2007)
Reply


Thread Tools
Display Modes

   Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 05:40 AM. Powered by vBulletin
Copyright © 2000 - 2007, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO © 2007 ProgrammersTalk Sedo - Buy and Sell Domain Names and Websites project info: programmerstalk.net Statistics for project programmerstalk.net etracker® web controlling instead of log file analysis


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50