Jump to content

how C++ really works internally?

- - - - -

  • Please log in to reply
6 replies to this topic

#1
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Hi

I found the below given code on this very useful link:
http://www.adrianxw.....Consoles4.html

First I think I have to include non-standard windows.h header file. I have questions about the bold statements. For example, system() is a function call and arguments are system dependent. In windows the command to clear the screen is system("cls") but the argument used to clear the screen in Linux would be different (I have been told this).

What is "HANDLE hOut"? If it were a function call then it would have parentheses after it? Is it some kind of declaration such as the one "float x, y,..."?

I understand these three bold lines are related. What is "hOut = GetStdHandle(STD_OUTPUT_HANDLE)"? It has a look of a assignment statement such as "Mean = x+y/2".

Then what about "SetConsoleTextAttribute(hOut, FOREGROUND_RED)"? It has a look of a function because it has parentheses and there are arguments within.


It would be really kind of you if you could help me with these queries. Thanks for your help and time. Please don't forget I'm a beginner, so please keep things simple.

Regards
Jackson


#include <windows.h>

#include <iostream.h>


int main()

{

[b]

HANDLE hOut;

hOut = GetStdHandle(STD_OUTPUT_HANDLE);

SetConsoleTextAttribute(hOut, FOREGROUND_RED);

[/b]

     

cout << "This text is red." << endl;      


SetConsoleTextAttribute(hOut, FOREGROUND_GREEN);

cout << "This text is green." << endl;      


SetConsoleTextAttribute(hOut, FOREGROUND_BLUE);

cout << "This text is blue." << endl;

return 0;

}


I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#2
DocSavage

DocSavage

    Newbie

  • Members
  • Pip
  • 7 posts

jackson6612 said:

First I think I have to include non-standard windows.h header file.
It may be non-standard unless you are working with windows, in which case it is standard, as are many other windows specific headers.

Quote

...system() is a function call and arguments are system dependent. In windows the command to clear the screen is system("cls") but the argument used to clear the screen in Linux would be different (I have been told this).
True.
That is one method of clearing a Console Screen.

Quote

What is "HANDLE hOut"?
It is a variable type declaration.
It declares that variable hOut is of data-type HANDLE.

Quote

What is "hOut = GetStdHandle(STD_OUTPUT_HANDLE)"?
It has a look of a assignment statement ...
Yes, it is an assignment.
It says:
hOut equals the returned result of calling function GetStdHandle() with the parameter "STD_OUTPUT_HANDLE".
In other words, after the function call, hOut will contain a Handle as its value.

Quote

Then what about "SetConsoleTextAttribute(hOut, FOREGROUND_RED)"?
Yes, it also is a function.

#3
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
HANDLEs are nothing more but plain integers, or that's what I've been told. To put it simple, each window has it's ID number. Also, in the code you've provided, there seem to be some errors: C++ libraries don't have .h extension (this is not really an error) and you're missing using namespace std; statement.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
Non-standard should be a term used sparingly by itself, non-standard to what exactly?

I have a feeling everything has been covered, although you will need to be aware many libraries or rather application programming interfaces (APIs) will define their own definitions of types and structures, you will have no clue what they are until you see their definitions.

An example of what HANDLE may be,
typedef __INT32 HANDLE
As you can see, HANDLE would simply be an __INT32 data type, it is defined as __INT32 as the HANDLE type will only need to use that range.

It could very well be a structure as well, in example:

typedef struct __WIN32_HANDLE {
   int id,
   int foobar,
} HANDLE;
And still, it will look the exact same in use, as the previous example:
HANDLE hOut;
So you should be aware that non-standard types like this can be anything, you should research what the type is so you are aware of how to properly handle them.

If you are concerned about portability of clearing the screen or colouring the console, then you should normally look for cross plaform solutions, then you do not need to worry how exactly each platform works. I believe a terminal manipulation library is Ncurses, although it may be named differently.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#5
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts

Flying Dutchman said:

HANDLEs are nothing more but plain integers, or that's what I've been told. To put it simple, each window has it's ID number. Also, in the code you've provided, there seem to be some errors: C++ libraries don't have .h extension (this is not really an error) and you're missing using namespace std; statement.

Hi Dutchman

Thanks a lot for pointing out the mistakes in my code. I couldn't understand the bold part. Could you please explain it simply a little more?

Alexander said:

Non-standard should be a term used sparingly by itself, non-standard to what exactly?

Thanks, Alexander. Rightly said. But isn't there a standard for C++ set up by ANSI. I'm a beginner and so far I have been told this. Please correct me.

Alexander said:

I have a feeling everything has been covered, although you will need to be aware many libraries or rather application programming interfaces (APIs) will define their own definitions of types and structures, you will have no clue what they are until you see their definitions.

Please help me here. Please use simplistic explanation so that I could fully appreciate your help. Thanks.

How would define a library? A set containing same type of function? e.g math library would contain functions such as sqrt().

API: I have tried several sources but couldn't get any general understanding of this. Do you have some simple explanation for a beginner like me?

int, floar, char are different data types. What is a definition and a structure?

Thank you for your help and time.

Best regards
Jackson
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#6
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
I think this is where you might find a good explanation of what a HANDLE is.
What I meant by what you've put in bold is that; image you have some programs opened and they're on your taskbar. Lets say you browser, chat program, your IDE and your program running. Now, each of those windows has it's own unique ID number and when you're programming you can say something like this:

myID = find out what number my IDE has

set colour scheme on window, whose ID equals to myID

I hope that clears it a bit more. :)
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#7
DocSavage

DocSavage

    Newbie

  • Members
  • Pip
  • 7 posts

Quote

But isn't there a standard for C++ set up by ANSI. I'm a beginner and so far I have been told this. Please correct me.

There are the ANSI C and C++ Standards and then there is Microsoft Windows.
They are two different things.
Anything that is Windows specific is considered non-standard with respect to the ANSI Standard.

Quote

How would define a library? A set containing same type of function? e.g math library would contain functions such as sqrt().
Google: "C/C++ tutorials". There are several on the internet.

Quote

API: I have tried several sources but couldn't get any general understanding of this. Do you have some simple explanation for a beginner like me?
int, floar, char are different data types. What is a definition and a structure?

If you are going to be programming for Windows, then you need to become familiar with the "Platform SDK".
The Platform SDK contains all the Windows API functions. Google that too.

A seperate library that also comes with Windows is the C-Runtime Library.
It contains all the basic ANSI C functions and all the Standard Math functions, as well.
I recommend looking on Amazon for some Beginners C Programming Tutorials. There are several.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users