brianSan
brianSan

Reputation: 535

C++: Do all functions and ivars have to be declared in the .h file?

I'm migrating from C to Obj-C into Obj-C++. I'll be taking my course in into to C++ for next semester. Hopefully, I'll breeze through it.

So in Obj-C, private and public is determined by whatever you declare in your header file. Within a @interface of the .h file and all properties and instance/class methods that you can call on that class are laid out for you. From what I can assume about C++, I feel like public and private is in fact, all written out in the .h file: ivars, functions, what have you. Am I right in this assumption?

Upvotes: 0

Views: 145

Answers (2)

Kevin Grant
Kevin Grant

Reputation: 5431

With earlier Objective-C compilers the @interface of a class (often declared in the header file, .h) had to contain all data members ("ivars"). With the newer LLVM compiler it is possible to list them in the @implementation (.m file).

Other parts of Objective-C classes are not so restricted. Methods can be declared or defined in .h, .m, both, or neither! Headers typically describe the methods that are most important, omitting things that are implementation-specific. Categories (a feature of the language) are a way to add new methods to a class while declaring and defining them somewhere else. There are also various ways to use NSObject and the Objective-C runtime to do almost anything you can think of, e.g. deciding at runtime whether or not a class has a method, or adding a method, etc.

Upvotes: 2

jimmyC
jimmyC

Reputation: 563

No, you can have public and private not in various .h files. Not sure about obj-c/c++, but if you do choose to put them in.h files (preferred in bigger programs), you need to #include the .h files.

Upvotes: 1

Related Questions