Learning C++, what's the syntax?!

This topic is 3881 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Share on other sites
anything having to do with the pre processor starts with a #
like #define or #include.

char **pName is a double pointer. These are really rare in C++ I would be suspicious of a tut trying to teach them to a newb.
[] is pretty much the key word for arrays. delete [] means you are trying to delete an array that was created by new []

<> is a little overused in c++ #include<> means check system headers before user headers #include"" is the opposite.

<> is also used to define templates. so vector <type> generates type safe code for the vector class that stores objects of type.

Share on other sites
Quote:
 Original post by ashmantleI recently started learning C++ through books and 3DBuzz's excellent C++ VTM's, but it just stomps me sometimes when they cover something that seems to break the C++ syntax.

I'm pretty sure it won't be breaking the syntax, otherwise you'd get a compile error.

Quote:
 A good example is double pointers.. I feel like I grasp the concept of pointers, but suddenly they throw in another asterisk and just assume that I understand that as well. Did the char **pName just become a new pointer to a pointer of type char? or is it a preprocessor directive? or is it an operator of all base types?Stuff like this confuses me.

That is called a pointer to a pointer. Basically it works like a pointer to another pointer to an object. You can dereference it like this:

int data = 0;int *pointerToData = &data;int **pointerToPointerToData = &pointerToData;**pointerToPointerToData = 10; // This sets data to 10

Remember that pointers and arrays are somewhat interchangable. So often when you see a **, it refers to a pointer to an array.

Quote:
 Then there's []. I understand arrays, but is the double brackets here a keyword for 'Array' or a preprocessor keyword? To clarify, I understand the *p usage, but not the 'delete [] m_buffer' usage.

The [] specifies that an operation is to be carried out on an array and is context sensitive. delete[] is necessary for deleting arrays, because array allocation on the free store (heap) is not the same as allocating one single object. It has nothing to do with the preprocessor, as far as I understand it.

Quote:
 Don't get me started on <>'s.I can't find any information about them in the C++ language references I've read, but they're all over the source code I've seen.You have them in include statements, but sometimes I've seen "" been used instead?You have them in the middle of operator overloading functions, for example when 3DBuzz tried to explain how to overload the << stream operator, they threw all the syntax I had learned and understand out the window and riddled the statements with all kinds of brackets.

<> is used in a number of contexts. Firstly in the context of #include <iostream>, it means that you are including a header file which is contained in one of the compiler's directories (you can also usually specify other directories for use with the <> notation in your IDE). It also relates to templates for example:

template < class T >class sampleClass {public:    T myT;};

Templates are used for generic programming in C++.

Quote:
 a good example is the std::list class(?).std::list varListthis looks like an ordinary variable declaration, but now there's <>'s instead of the normal ()'s..

A perfect example of template metaprogramming.

Quote:
 Could anyone help me shed some light on this, my hair is turning gray :)

Share on other sites
Quote:
 Then there's []. I understand arrays, but is the double brackets here a keyword for 'Array' or a preprocessor keyword? To clarify, I understand the *p usage, but not the 'delete [] m_buffer' usage.
Has nothing to do with the preprocessor. *p is saying give me the object that is pointed to at index i in p (p is an array of pointers to some object). delete [] is the syntax used to delete an array. You use the normal delete when whatever you're deleting is not an array. Just like new [] and new.
Quote:
 Don't get me started on <>'s.I can't find any information about them in the C++ language references I've read, but they're all over the source code I've seen.You have them in include statements, but sometimes I've seen "" been used instead?
<> in include statements are for header files that are local to/come with your IDE. You use "" whenever you want to include something that IS NOT local to your IDE.
Quote:
 a good example is the std::list class(?).std::list varListthis looks like an ordinary variable declaration, but now there's <>'s instead of the normal ()'s..
In this context, the <> represents a templated class or function. Templates are very advanced and you shouldn't worry about them if you're just learning. Just accept that with them, you can use any type of data. EG:
std::vector<int> int_vector;std::vector<std::string> string_vector;std::vector<double> double_vector;
etc.

Hope that helps.

Share on other sites
wow!

I never expected such well crafted replies, and so fast!
Thank you everyone, you've given me lots of material and I believe some of it is starting to sink in finally :D

Now I just need to research Templates. I find it hard to use a language when I don't know WHY stuff works :)

Share on other sites
here is the template page from one of the c++ ref sites I like.
Here

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5
frob
13

• 16
• 9
• 31
• 16
• 9
• Forum Statistics

• Total Topics
632613
• Total Posts
3007431

×