• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

About C++

Status
Not open for further replies.
Hello Hivers!, Im self studying C++ slowly and I want this thread to be as open as possible for C++ only, means not Java, Haskell, C, C# etc...

I want to ask questions if you don't mind;

- is #include means uses/needs/requires in vjass ?
- is #include <iostream> and a-like a library in vjass ?
- is int main() works like onInit or initializer in vjass ?
- all codes in C++ always contain int main() ?

Maybe I will ask more in the future but that's all for now...thank you in advance!

EDIT:
I may compare this to jass/vjass to understand it better...
 
Last edited:
Yes, #include is similar to requiring a library in vJASS. It will allow you to use the code from that header file (.h).

As Blinkboy said about main(), it depends. However, you can still view it as an initializer. It is usually where everything begins--and you can organize how your program runs and perform whatever preliminary setup that you'd like to do.

You can also utilize its parameters (int argc, char **argv) to control what happens when you run the tool via command line.

means it can return any value? not explicit?...

Well, the return type should be int, since it is declared as "int main()".

What crazed_seal meant, though, is that the return value can determine the exitcode behavior. You don't necessarily need to return 0.

normal exit -> usually return 0
not normal -> return non-zero number

However, this is usually just for your own testing. There are more explicit notations for the above: EXIT_SUCCESS and EXIT_FAILURE, defined in cstdlib. I recommend using those if you want to make your code more clear.
http://en.cppreference.com/w/cpp/utility/program/EXIT_status

You can also read up on stack overflow if you have any miscellaneous programming questions. Chances are someone has asked it. :)
 
- is #include means uses/needs/requires in vjass ?
- is #include <iostream> and a-like a library in vjass ?
- is int main() works like onInit or initializer in vjass ?
- all codes in C++ always contain int main() ?

- #include is a preprocessor directive that copies the contents of a given file into your source file. We use it to keep function and class declarations so they are usable in our code. You can't call a function if it hasn't been declared yet. Compilers usually assume that the return type of a function is int if they have not been declared yet.

- #include <iostream> will copy the contents of the iostream file into your source file. The iostream file contains all the declarations (and some definitions) of standard stream facilities and it contains the instances of the standard input and output streams (and std::cerr)

- int main is a user-defined function called by _start (On Unix-based systems for all I know). _start will set the environment for the program to run in. There are so many things that happen before int main: The initialization of global variables, the calling of the constructors of static class instances (std::cin and std::cout are initialized for example), and so much more. _start will pass whatever command line arguments the OS piped to it to int main (The OS is actually the caller of this function. The kernel is responsible for this in most OSs I know of).

- There can only be one int main per program. All C++ programs need to have int main defined (I'm talking about portable programs. Windows-specific applications can start with WinMain).

You can change the entry point of a program using -e command when compiling with GCC if you wanted to, but do note that this will not change int main, it will change int _start.

Oh, also, I forgot to mention that whatever int main returns is taken by int _start and returned to the Operating system. The Operating system takes this data and does whatever it wants with it. 0 denotes success and anything else denotes failure.
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,258
- is #include means uses/needs/requires in vjass ?
It means insert the following file character for character here. It is done during compile time and is a good way to put headers in to your code. Since header files define the interface, you can limit the coupling of code files to only be exposed to the headers you need to use.

- is #include <iostream> and a-like a library in vjass ?
It is a special include syntax used for standard C/C++ libraries. It works like regular include but checks a different file path for the files. The library code is usually stored with the compiler.

- is int main() works like onInit or initializer in vjass ?
It is the entry point for process initialization. Only one such function can exist and it is called exactly once by a thread when the process is created. It also takes a character pointer of some kind as an argument. This argument is given the value of any command line arguments you use when starting the process.

- all codes in C++ always contain int main() ?
All C/C++ programs always contain exactly one int main function which is the entry point of the program when the process is started. Libraries do not require such a function as the operating system handles them differently.
 
Well, the return type should be int, since it is declared as "int main()".

I tested char and it didn't pop errors, well I think coz char defines letters as intergers ini ASCIIs?...

- There can only be one int main per program. All C++ programs need to have int main defined (I'm talking about portable programs. Windows-specific applications can start with WinMain).
needs and must, which is it?, like vjass, a library doesnt need to have an initializer...

Okay now for my next questions;
- can you show me how to call a function from, say main.cpp to test.cpp, just a small code pls...
- some code do like #include "blahh.h" instead of #include <???>, now are those in "" and <> a file name?...
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,258
I tested char and it didn't pop errors, well I think coz char defines letters as intergers ini ASCIIs?...
I think that is because of C/C++'s automatic type casting up. A char is usually 8 bits (a byte) so can be type cast up to short, int long, long long etc automatically.

Char is used to refer text usually as a standard, not that it is required. Something that takes char* usually is a string and char** is an array of strings. Because 8 bits is not enough, there is also w_char which is 16 bits in size and is a different type of string. The size of char and w_char might also be platform dependant so more or less than their standard size, which is another reason they are separate from the primitive types.

needs and must, which is it?
As far as I am aware they both mean the same thing. "Need" is a rather strong word, so is "must".

like vjass, a library doesnt need to have an initializer...
Neither does a C/C++ library since those contain nothing but program code for processes to use. However a C/C++ program does since it is the entry point on process creation.

- can you show me how to call a function from, say main.cpp to test.cpp, just a small code pls...

Needs 3 files...
test.h
Code:
int test( void* arg1, void* arg2 );

test.cpp
Code:
#include "test.h"
int test( void* arg1, void* arg2 ){
    return (int)(arg1 + arg2);
}

main.cpp
Code:
#include "test.h"

// somewhere inside a function inside main.cpp
int i = test(0, 0);

You will need to make sure that your object code file from main.cpp and test.cpp are both used during linking so that the full program is made. Especially when using gcc/g++ you may need to perform Makefile modifications.

- some code do like #include "blahh.h" instead of #include <???> , now are those in "" and <> a file name?...
I already answered that...

- is #include <iostream> and a-like a library in vjass ?
It is a special include syntax used for standard C/C++ libraries. It works like regular include but checks a different file path for the files. The library code is usually stored with the compiler.
#include "blahh.h" is used for including your program header files. Also be aware that #include is mostly used for header files at the top of a code file but nothing prevents you from including code files or using it anywhere in the code file that is syntactically legal. Generally you should only us it for header files at the top of every code file as this has good readability.
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,258
Atm Im using code blocks and hopefully someday buy visual studio...
I could swear the basic version is free.

Is int blah(string str, int x) same as function blah takes string str, integer x returns integer ?
Sort of. However do remember that JASS and C/C++ are completely different so making a 1:1 comparison is impossible.
 
Level 23
Joined
Apr 16, 2012
Messages
4,041
the differece between #include <myAss.h> and #include "myAss.h" is that include with "" checks first the folder your .cpp that checks is located and then the default location of scripts(those are most of the time only the standard ones and if you have windows then some windows stuff as well) and <> only checks the default folder set in compiler

For instance in Visual Studio(herr herr Ima pirate!) you can give the compiler additional routes to search for headers in <>, for instance if you have big project and the headers are in different folders, you can put the path to the folder there and he will search that folder for the files as well

and yes visual studio 2010 is free to some degree(I mean, there is free version of it on microsoft website)
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,258
Either you are using a pirated operating system (which is a very low thing to do...) or your computer is infected with malware that stops you doing anything Microsoft related as a defence mechanism. The fact you say it is not updated makes the chance of a virus more likely.

I'd like to know also what game development kit you recommend?...

Windows has the DirectX and other standard components for input, graphics and sound. There are various Open equivalents like OpenGL which are more portable and universal.
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,258
- Are there any natives in C++ or all of it are just libraries/wrappers?
There are kinds of natives that come in the form of platform specific libraries. These usually are created by C or Assembly by the operating system producer which interface directly with the kernel to perform functionality such as I/O, page management, resource management, data transfer, etc. They are not really called natives but since they provide the lowest level functionality of a platform you can view them as natives.

Since platform coupling is bad, it is recommended to use a platform independent adapter of such functions.

- Are there links to all libraries like iostream, sstring, etc...
5 seconds on Google will show you them all. There are only a few, any advanced functionality will need platform dependant libraries.

- How to write/declare globals?
Same way as in StarCraft II. They are the same syntax as locals except outside a function scope. If you want them to be used from a header yet implemented in a cpp file you need to declare them as "extern".

- How to declare arrays?

Size known at compile time
int NAME [size];
int NAME [] = {constant int statements here...};
int NAME [number of statements] = {constant int statements here...};

Size known at runtime
int * NAME = (int *) malloc(size * sizeof(int));

C++ offers you various standard data structures that act more like WC3 arrays but obviously at the expense of performance.

C/C++ does not check array (memory) bounds. The following is valid but will likely throw a permission or segmentation fault and in any case is very unsafe behaviour (the type of thing exploited by viruses).
int NAME [10];
NAME[99999] = 0;
 
it is clear you haven't read a single resource about C++
you though wrong, I have been watching youtube videos and learncpp and cplusplus websites for tutorials but cant see how they declare globals and arrays...all I want from here are little info on how to create them, the rest is up to me to test it...like DSG (thanks to him btw) sample of array creation, now I know...

It is not like everything has to be object orientated.
true, that's why I asked for natives :D...
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,258
Don't use globals. At all.
You don't want to introduce global state into your application. Unless you have really good reasons, don't use them.

They should be used for process wide content or when dynamic memory is too expensive. Process wide content might be a resource cache (eg, for I/O through a network) and process settings (debug log, performance profiles etc). Platforms with very little memory or too slow to use dynamic memory allocation such as microcontrollers almost exclusively use globally allocated memory as it is the only way to define persistent state, however as the scope of the program is well defined you have very little dynamic nature to worry about and anything outside the available resources can be defined as an error state. StarCraft II falls under such a limited resource system as it has no dynamic memory allocation so most game stuff has to be globally defined.
 
Classes are like vJASS structs in terms of purpose, not in terms of semantics and implementation. Also, there are structs in C++. struct is a valid keyword.
The only difference between structs and classes in C++ is that struct members are public by default while class members are private by default.

You can have as many instances of a class as you want. Your only limit is RAM.
Also a class is not represented as an integer for an array, it's a block of memory on its own with a size equal to or greater than the sum of the sizes of its members.

Padding is often done as an optimization for speed rather than size.
Compilers might add to the size of a class or a struct for whatever reason they want as long as they follow the standard.
Do note that Microsoft doesn't follow the standard very closely so you may find some inconsistencies when working with their compiler and IDE.
(This is why I recommend the use of something like Code::Blocks or simply GCC and a good text editor)

As for initialization, the second method is better in general and it's sometimes even required.
In your case, the difference is absolutely nothing. The generated assembly will be exactly the same for both.

Consider this:

C++:
class foo {
    really_big_object bar;

public:
    foo(const really_big_object& p_bar) {
        bar = p_bar;
    }
};

Let me tell you what happens when you create an instance of Foo with the given constructor

  1. bar is default initialized (This calls the default constructor).
  2. The foo constructor is called.
  3. bar is assigned p_bar (This calls the copy assignment operator).

Notice the redundancy? We're calling 2 constructors for an object. Initializer lists are the solution to that:

C++:
class foo {
    really_big_object bar;

public:
    foo(const really_big_object& p_bar) : bar(p_bar) {}
};

This is what happens here:

  1. The copy constructor of bar is called upon initialization of the class instance.
  2. The foo constructor is called.

The difference is actually noticeable to a certain degree. Depends on what your program is doing.

Another use case is when you want to initialize const members:

C++:
class foo {
    const int value;

public:
    foo() {} // compiler error
};

Solution:

C++:
class foo {
    const int value;

public:
    foo(int k) : value(k) {} // works
};

Initializer lists are also required to resolve ambiguities with constructors:

C++:
class foo {
    public:
        foo(const std::string& file_name = get_default_directory());
        foo(int x = 0, const std::string& str = "Lorem ipsum");
};

class qux {
    foo baz;
public:
    qux() { // compiler error: foo has multiple default constructors
        baz = foo("/textures/mario.png");
    }
};

Solution:

C++:
class foo {
    public:
        foo(const std::string& file_name = get_default_directory());
        foo(int x = 0, const std::string& str = "Lorem ipsum");
};

class qux {
    foo baz;
public:
    qux() : baz("/textures/mario.png") {} // OK
};
 
Last edited:
No, pass by reference or reference to const.
Don't pass by pointer if you don't need to.

If you pass by pointer, you have to check the state of the pointer passed. With references, at least you're guaranteed to have an object. There is no such thing as a null reference, but there is a null pointer.
(Yes, there is such a thing as a dangling reference, but that only happens when your code is incorrectly managing the lifetime of objects. Use RAII to manage object lifetime and you'll be fine.)

People argue that when you want a null value, you pass by pointer, but even this isn't the best solution nowadays.
If you use boost, which you should, if there is a null state for an object, you should pass a boost::eek:ptional<T> (C++14 will bring std::eek:ptional)

If you want a null object, you pass in boost::none
 
Just wanna comment on the visual studio part, the express editions are free (the 2010 version has C++ included in the package)... and it already has a lot of capabilities at that, and all your creations are yours... IDK about the newer editions though (which you might wanna pick if you want to go with developing apps that utilize the win8 metro)
 
No, pass by reference or reference to const.
Don't pass by pointer if you don't need to.

If you pass by pointer, you have to check the state of the pointer passed. With references, at least you're guaranteed to have an object. There is no such thing as a null reference, but there is a null pointer.
(Yes, there is such a thing as a dangling reference, but that only happens when your code is incorrectly managing the lifetime of objects. Use RAII to manage object lifetime and you'll be fine.)

People argue that when you want a null value, you pass by pointer, but even this isn't the best solution nowadays.
If you use boost, which you should, if there is a null state for an object, you should pass a boost::eek:ptional<T> (C++14 will bring std::eek:ptional)

If you want a null object, you pass in boost::none
I would like to add something:

actualy a "null pointer" doesn't exist, it's just a pointer that is not pointing to anything (in theory). Checking is not that much needed if you make a good design of your program from start, by defining what are the conditions for inputs and outputs of each function. If you always respect those conditions on your code, you'll have to worry less of dangling references or memory leaks. But as always, if your program does not need to be extremely fast (hardcore real time), then using a garbage collection library is suggested.
 
But as always, if your program does not need to be extremely fast (hardcore real time), then using a garbage collection library is suggested.

Yes, to be more specific, mckill, I recommend smart pointers like shared_ptr and unique_ptr so RAII takes care of the deallocation of allocated memory.

actualy a "null pointer" doesn't exist, it's just a pointer that is not pointing to anything (in theory)

Pretty much, yes

Checking is not that much needed if you make a good design of your program from start, by defining what are the conditions for inputs and outputs of each function. If you always respect those conditions on your code, you'll have to worry less of dangling references or memory leaks.

Well sure, you can avoid the checks, but I'm not so sure if they should be avoided when you're designing a system specifically for other people to use ;o
Do you believe those checks should be removed in such a case? (Yeah, I'm aware that people should RTFM but I'm not so confident that these kinds of checks should be removed using "RTFM" as the excuse :/)

I'm actually indifferent. I'd still use a library even if it didn't have proper safety checks. They would be nice though (throwing exceptions is cool too~)
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,258
Only the external interface of a library should preform bound checks. Internal communication (which you have full control of) does not need bound checks as it is assumed you will not mess it up. You do, however, have to clearly define the expected parameters so that future developers do not use incorrect values.

The idea is that a small number of external interface calls use a large number of internal calls. By saving on bound checking on the internal calls you will save considerable processor time that should not be necessary. The external interface call bound checks use so little time that you can ignore them.
 
I know about the host file that is capable of blocking Ip addresses, and my host file is clear of that...

The thing is I really cannot do it...

I did this;
attachment.php


Then it appeared;
attachment.php


Even yahoo;
attachment.php


I even used IE and Firefox but still cant access these websites, I was thinking maybe coz of my ISP...
 

Attachments

  • Clipboard01.jpg
    Clipboard01.jpg
    17.3 KB · Views: 202
  • Clipboard02.jpg
    Clipboard02.jpg
    7.3 KB · Views: 164
  • Clipboard03.jpg
    Clipboard03.jpg
    9.7 KB · Views: 173

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,258
If your in China or a country behind a firewall you will need to make sure that you use the local version of the site as they often block American sites. If you are in a country with sanctions on it then it is impossible to update as MS is not legally allowed to service sanctioned countries, even if you brought the software before the country was sanctioned.

It is also possible your DNS is rather poor. Use a better DNS such as that offered by google and it may work. Also some malware infections will prevent you from accessing certain sites and searching in order to stop you from fixing the infection.
 
Level 29
Joined
Jul 29, 2007
Messages
5,174
Note that running on Google's DNS server destroys the concept of CDNs (unless you live next to the DNS of course), and thus you will get performance hits in sites like Youtube (or really any big site).

Not that this matters if your DNS is too bad to actually use, but if this really turns out to be the problem, you might want to find a free DNS service closer to where you live.
 
Configure your network settings to use the IP addresses 8.8.8.8 and 8.8.4.4 as your DNS servers or
never really tried to do this manually before but it is safe?, maybe after doing that I cant connect?, I use wireless btw, I can connect via cable also...

Ok for my next questions;
1. Variables in a class should be private and can should be accessed only on functions?
2. If a struct is public and class is private, I should use class always since I cant allow my program to be accessed by anyone?
3. Classes is best situated on headers not on cpp?
 
Level 23
Joined
Apr 16, 2012
Messages
4,041
all of those three are design choises, it doesnt matter which you do

1. You can make all variables private and accesable by getters/setters only, but some people dont like that, or you can make everything public

2. you can explicitly declare which accessablity(is that word?) you want, if you do
C++:
struct mystruct{
    private:
    int i;
};
its the same as doing class myclass{int i;};
so again, this is only up to your preferences

3. Again, depends on yourself, many people split their classes to both headers and cpps(definitions in headers, declaration in cpp files), I personally just write them where its closest to me(but Im trying to make bigger classes as its own header)
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,258
never really tried to do this manually before but it is safe?, maybe after doing that I cant connect?, I use wireless btw, I can connect via cable also...
The DNS is run by Google so should be safe. If you try it and then you find it does not work you can simply restore to defaults (the DNS you currently use which may be automatically acquired).

1. Variables in a class should be private and can should be accessed only on functions?
Private variables can only be accessed by methods defined within the class or other classes that the class gives private permission to. They should be used for internal state information you do not want as part of the class's interface to reduce unnecessary coupling.

2. If a struct is public and class is private, I should use class always since I cant allow my program to be accessed by anyone?
Both structs and classes can be public or private within some named space.

Member wise structs should only have public members because a struct defines a platform specific way to interpret memory. Classes can mix both public and private members as they define an abstract platform dependant way to interpret memory.

The key thing to note is that you define how memory is broken up with a struct while the compiler does with a class. If you define struct with 3 members then you will have exactly those 3 members converted to memory (with maybe some padding for word alignment). If you define a class with 3 members, the compiler may make 3 or more pieces of data to represent the class in memory. Some of this extra data includes class information, inheritance information, method pointers etc (generally stuff needed to support OOP).
The keyword "private" purely hides the variable from a programming point of view to reduce coupling. It is not a way to protect your code by keeping it secret.

3. Classes is best situated on headers not on cpp?
In most cases you will define the class interface in a header file. This is so that cpp files can use the class by importing the header.

The implementation of the class (where the methods are defined) should be in a cpp file. This is to keep the header clean of program code and take advantage of object code files for more efficient compiling. It will only compile your method implementations once and then use the resulting object code file every time another cpp file references the class.
 
Status
Not open for further replies.
Top