-4

1.what is the use of -> for exemple is test->name the same as test.name?

2.What are the header files used for? I am used to other programming language like java where you only need 1 file(the class file). Do we neessarily need to use a header file to declare all the stuff?

John
  • 131
  • 5
  • 14
  • 10
    Both these questions would be answered in the first few chapters of any [decent introductory book on C++](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Oliver Charlesworth Feb 25 '12 at 16:48
  • 1
    @OliCharlesworth what he said was actually the best response to that that I've ever heard... – Seth Carnegie Feb 25 '12 at 16:51
  • 1
    well then any decent beginner tutorial online. just search for "beginner c++" – UmNyobe Feb 25 '12 at 16:52
  • 1
    I'd recommend this, since you're coming from java: https://docs.google.com/viewer?a=v&q=cache:BvpZbmQvuCYJ:citeseerx.ist.psu.edu/viewdoc/download?doi%3D10.1.1.132.6953%26rep%3Drep1%26type%3Dpdf+&hl=en&gl=us&pid=bl&srcid=ADGEESjMRvTja6ZcimvPTIOmBJGqIdZsDzLJBkDjmSqyt_AhC0hc0Mazl9D0Qs2PMaN6XDobAcLmr7ypWq-cj9on5o1c91FugLhmum0ro2w2Jt7z5qRrGHJGVTCIzFMZiuPfedeWt5YT&sig=AHIEtbRTzaliFAPbXhDyUSMceaWqi9gs-w&pli=1 – holtavolt Feb 25 '12 at 16:53
  • I actually remember struggling to understand the headers when I was starting with C. And many books will not explain it. I've been doing C++ for over 12 years so it's very natural now, but it wasn't so 12 years ago. – Correa Feb 25 '12 at 17:06
  • @SethCarnegie: I'm not sure what you're suggesting. Trying to get started with C++ without any kind of introductory reference is a waste of time... – Oliver Charlesworth Feb 25 '12 at 17:08

7 Answers7

4
  1. a->b is the same as (*a).b when a is a pointer. It's just convenient syntax for the same thing. When a is a class, it calls the operator-> of the class with a as the invoking object. If you don't understand that, then don't worry, when you get further in C++ you will.
  2. Header files are to contain declarations while .cpp files contain the definitions. You need them when you want to write functions that can be used from other files. This is because you can have as many declarations of a structure (that is, a variable, function, class, etc) as you want, but only one definition of that structure. Header files are included many places in the code (by every file that needs to use the facilities its implementation provides) and are compiled once for every .cpp file that includes them, so it wouldn't do to put anything you can only have one of in a header file. Implementation files (.cpp or .c files) are only compiled once for the whole program, so they are the right place for the implementations (things that you can only have one of). Header files are also to facilitate the seperation of implementation and interface.

However, all these questions would have been answered by a good C++ book, so please buy and read one before going too far, it'll save a lot of time and headache.

Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
1

structure->member

is the equivalent of

(*structure).member

thus, it is a shortcut. It dereferences the pointer structure and accesses member member

nerozehl
  • 463
  • 1
  • 7
  • 19
1

1) The "arrow" operator -> is not the same as the operator ., but a shortcut when you use pointers to structs instead of just structs. See this explanation, for example:

struct Point {
    int x;
    int y;
};

Point* p;      // p points to a Point struct

p = new Point;
p->x = 12;     // This is an useful shortcut for...
(*p).x = 12;   // .. this

2) About header files, they aren't necessary, you can also add the declarations at the beginning of the .c file, but it's an useful convention to have declarations and actual code seperated.

schnaader
  • 49,103
  • 10
  • 104
  • 136
1

Well, I will try to explain in a simple way to make it easier for a beginner to understand better.

  1. Yes, it's the same as dot, but it's used for pointers.

    MyClass a;
    a.myMethod();
    
    MyClass* b = new MyClass;
    b->myMethod();
    delete b;
    

    Both a and b call the same method, but a is allocated in the heap and b is a pointer allocated by new. Memory explicity allocated should always be deallocated (thus the delete b statement). This is actually a simplification, what the arrow does is dereference the pointer and call the dot operator over it. If this makes your head spin at the moment, just take the simple explanation for now, it will sink later as you became more acostumated with the language.

  2. Header files are not obligatory, but they are usefull to reference what was declared in translation unit in another one. So, for example, you declare MyClass in a header, than you can use it in both myfilea.cpp and myfileb.cpp without too much effort having to declare it more than once.

    To properly understand the header files you need to know that when you use statements like:

    #include "myheader.h"
    

    What the compiler's preprocessor does is copy and paste all the contents of myheader.h into the file that included it. Since the compiler needs to know about the types (usually classes) before letting you use them, in each file you need to "redeclare what the types are". It's entierely possible to implement everything inside the header file actually, similar to what would look like a Java code, then in one main.cpp file include those files, but then the code would take a lot longer to compile and would be harder to follow.

Correa
  • 402
  • 2
  • 11
  • a is unlikely to be allocated on the heap. – tinman Feb 25 '12 at 17:15
  • Sometimes it is required to have definitions separated from declarations. Per example, in circular patterns, i.e. if `A` requires a `B` instance and `B` requires an `A` instance, you have no other choice but to separate the implementation from the declaration to avoid errors like double definitions and usage of incomplete types. – netcoder Feb 25 '12 at 18:27
0

I think you need to read a book on C++ and C, '->' is not the same as '.' but almost, and header files has to do with the compiler, and the memory layout. C++ is alot more about the actual machine than java, and the means that you have to thinkabout programming in a slightly different way.

I recommend reading Bjarne Staustrups book on C++, or at least browse through it. If C++ was just Java with another syntax there would have been no reason to invent Java -- The languages each have their applications and you need to understand what the language is about before you start using it. :-)

Martin Kristiansen
  • 9,875
  • 10
  • 51
  • 83
0
std::string str("foo");
std::string *str_p=new std::string("bar");   /
//Please use smart pointers in real code
std::cout <<  str.size() << "   " << str_p->size();

so basically -> deferences a pointer than access a member of the type.

It is equivelent too

(*str_p).size(); 

here str_p is first dereferenced manually then the size member function in invoked.

111111
  • 15,686
  • 6
  • 47
  • 62
0
  1. test->name is completely equal to (*test).name , not test.name
  2. The headers are used to include the same definition in many translation units, Java don’t use them because definitions are never required, all objects are used by pointers.

Look at this example:

class A
{
   B member;
};

If you don’t include the definition of B, you cannot know the size of class A. In java, member would be a pointer, so this is useless.

qdii
  • 12,505
  • 10
  • 59
  • 116