Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hanks Homework, #9

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

hankofwisdom
Copy link

Are we going to learn Pointers math
Why dose auto on an iostream make it an integer?

#include <fstream>


//input = std::cin , output = std::cout; Can this be done?
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can put using std::cin; and using std::cout; up here and then just write cin and cout below.

@ninjapanzer
Copy link
Member

@hankofwisdom what was an example of a statement where you were using auto with ifstream?

@hankofwisdom
Copy link
Author

@ninjapanzer it was the top comment I read the iostream class and it won't work

@ninjapanzer
Copy link
Member

cin is declared as as iostream and then applied to tie to bind it to the std stream. But I will have to try it later.

@ninjapanzer
Copy link
Member

So the reality is that C++ also includes a move constructor because streams like CIN cannot be copied. Move rebinds the internal std::Tie class to the new stream object.

But it turns out there is a defect in how basic_streams are handled.
http://cplusplus.github.io/LWG/lwg-defects.html#911
From response http://stackoverflow.com/a/20774666

That said by using = implies a copy constructor due to operator overloading. You might be used to languages that hide references and pointers behind variable names. Anything that is not a primitive is actually a reference so when you do Object<thing1> var1 = var2; You are actually manipulating the reference of var1 instead of duplicating it. So here is an example of how a pointer could be employed to create an "alias" of the original variable. And auto does nothing but infer the type so these two statements are equivalent.

auto ins = std::cin;
istream ins = std::cin;

Both try to invoke the disabled copy constructor and thus fail because you can't copy a stream. Of course they provide a move method that is also basically disabled due to defect.

Pointers do simplify this and here is an example of basically what every other pointer abstracting language does https://gist.github.com/ninjapanzer/aa9cdd6f0d6a34d1f0df

#include <iostream>
#include <fstream>

std::string name = "",email = "",passcode = "",time_zone = "",file_path = "";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dangerous global variables that are not const could lead to data side-effects an difficult debugging. These should be within you main function or const.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants