I have a lightweight c++ class needed to be ported to Objective-C to exploit the facilities provided by NSObject. There will be thousands of instances of the C++ class, and i dont want to add to much overhead by wrapping it in a NSObject derived class.
how big is NSObject?
First of all, you should descend from NSObject. There is no tangible benefit to not doing so. NSObject adds nothing in terms of storage over the lowest level representation of a class but adds an awful lot of behaviour that will allow your classes to act correctly with the runloop, with the normal collections (eg, NSArray or NSDictionary) and with just about everything else.
To answer your question specifically:
#import <objc/runtime.h>
...
NSLog(#"obj size: %zd", class_getInstanceSize([NSObject class]));
Outputs '4' on iOS and '8' on 64-bit OS X. So the answer you're looking for is 'the natural size of an integer'. NSProxy is the same size, UIView (on iOS) is 84 bytes.
Documentation for class_getInstanceSize is here — it explicitly returns "[t]he size in bytes of instances of the class cls", as the name says.
in the apple documentation it says here that
The root class of all Objective-C classes is NSObject, which is part
of the Foundation framework. All objects in a Cocoa or Cocoa Touch
application ultimately inherit from NSObject. This class is the
primary access point whereby other classes interact with the
Objective-C runtime
So I'd say that every objective-c class ultimately derives from NSObject anyway, so you have no choice. Please, correct me if I'm wrong.
EDIT:
What I've learned now is that NSObject is one root class, which implements the basic behaviour expected from every objective-c class. So basically you could write an objective-c class which implements the protocol of NSObject (I think that is objective-c speak for interface) and implement basic stuff like alloc and init yourself. Be sure to check out this thread: How to instantiate a class in Objective-C that don't inherit from NSObject
EDIT:
Some more insight into root classes in objective-c, very interesting!
Related
Basically, I don't know much C++, and I've been trying to implement the leaderboard code from the friendSmasher sample project on github. After searching for some related posts, I found this post which recommended creating an Objective-C class to manage the data instead. Below is the relevant code from the project. How can I make an objective-c class(es) to manage the data?
struct LeaderboardboardInstance
{
System::Sprite* pStubSprite;
UILabel *pFriendName;
UILabel *pFriendScore;
System::TextureResource* pUserTexture;
System::Sprite* pUserSprite;
};
Also I believe the System::Sprite* abc and the other System calls are C++ libraries and would like to know the equivalent classes in Objective-C that I should use.
I hope this question is not too silly, but what is the most basic class in standard C++?
object? Object?
class MyObject : public object{ ...
and I get "Expected class-name before token{"
Is there any map, diagram or image that shows standard c++ classes inheritance?
Something like this but for C++ ?
There is no most basic class in C++ i.e. there is no common base class for all the classes.
There is no fundamental object type in C++, unlike in e.g. Java.
In Cocoa, the NSObject class is fundamental to the framework but not to the Objective-C language itself. In Objective-C, it is possible to create a root class by not deriving from anything (but in order to make it work you'll probably have to hack your way through runtime calls).
Similarly, some C++-based frameworks may define a root class that all other classes in that framework derive from, but it is specific to the framework, not the language.
This is the simplest most basic class you can compile:
class Null
{
};
Inheritance diagram for IOstream library is here. STL is a template library and doesn't use OOP.
Normally most of the MFC (window based classes) are derived from CObject.
What does CObject do?
Why some MFC classes are not required to be derived from CObject?
According to MSDN CObject mainly provides these features:
Serialization support
Run-time class information
Object diagnostic output
Compatibility with collection classes
So when none of this is needed, there is no need to derive from CObject.
There a bit of overhead (DECLARE/IMPLEMENENT_SERIAL/DYNAMIC macros) involved when deriving from CObject, too, so there may be simpler ways of doing things than by deriving from CObject.
The MFC documentation covers this pretty well. The documentation for CObject describes what it does (serialization support, runtime class information etc).
The Hierarchy Chart is a good overview, and shows which classes aren't derived from CObject.
You can conclude that the classes that don't derive from CObject are the ones that don't need the services it provides. The reasons are various: for example a class like CFileTime is a simple data type.
I need to finish others developer work but problem is that he started in different way...
So now I found in situation to use existing code where he chooses to inherit a non-abstract class (very big class, without any virtual functions) that already implements bunch of interfaces or to dismiss that code (which shouldn't be to much work) and to write another class that implements interfaces I need.
What are the pros and cons that would help me to choose the better approach.
p.s. please note that I don't have to much experience
Many Thanks
Although it is very tempting to say write it from scratch again, don't do it! The existing code may be ugly, but it looks like it does work. Since the class is big, I assume there is fair bit of history behind it as well. It might have solutions for some very obscure cases which you might not have imagined till now. What I suggest is, if possible first talk to the person who developed that class, understand how it works, then derive from it (after making its destructor virtual of course) and complete your work. Then as and when time permits slowly refactor the parts of the class into smaller more manageable classes. Also, don't forget to write a good unit-tester before you start so that you can validate the new behavior against the existing class's behavior. One more thing, there is nothing wrong in inheriting from a non-abstract base class as long as it makes sense and the base class destructor is virtual.
If the other developer has written a base-class with no virtual functions, then those functions do not need to be overridden, and it is correct to define them in a non-abstract base class.
If those functions define functionality that all the child-classes require then it would be a mistake to get rid of the base class, as you would then need to implement those functions individually in each of the child classes.
I've seen a lot of developers go 'interface-mad' in the last couple of years, but base classes still serve a function over interfaces - to provide a concrete implementation that is common to all child classes. It would be a mistake to get rid of the base class and have seperate implementations of these functions in each of the child classes.
HOWEVER, if the child classes are inheriting functionality that they do not require, or require a separate implementation of, then the Base class is a mistake and interfaces would seem like the better option to divide the functionality between the child classes.
Despite this, I would agree with Naveen that its probably not worth the extra work this will give you, it may seem simple, but if this is a big class with a lot of inheritors then it could turn out to be a nightmare. Quite often in Software Engineering you have to deal with another developer's code that you might have implemented differently. If you re-implemented it ever time you will be a very unproductive developer. I say work with what you've got and get the project finished on time.
Is there anything at all you want to use from the base class or would you end up overriding everything?
Does it define some sort of type that you want to use for an "is-a" relationship?
(for example, base class is "animal" and you want to make "cat", but if it doesn't add any behavior to its interface, that doesn't seem likely)
Is the base class used in other interfaces you need to use? (like if someone is passing objects through a reference/pointer to the base class)
If not, I'd say there's no advantage in inheriting from that class over implementing the interface(s) yourself.
What are the pros and cons that would help me to choose the better approach.
It's legal to derive from a class with no virtual functions, but that doesn't make it a good idea. When you derive from a class with virtual functions, you often use that class through pointers (eg., a class Derived that inherits from Base is often manipulated through Base*s). That doesn't work when you don't use virtual functions. Also, if you have a pointer to the base class, delete-ing it can lead to a memory leak.
However, it sounds more like these classes aren't being used through pointers-to-the-base. Instead the base class is simply used to get a lot of built in functionality, although the classes aren't related in the normal sense. Inversion of control (and has-a relationships) is a more common way to do that nowadays (split the functionality of the base class into a number of interfaces -- pure virtual base classes -- and then have the objects that currently derive from the base class instead have member variables of those interfaces).
At the very least, you'll want to split the big base class into well-defined smaller classes and use those (like mixins), which sounds like your second option.
However, that doesn't mean rewrite all the other code that uses the blob base class all in one go. That's a big undertaking and you're likely to make small typos and similar mistakes. Instead, buy yourself copies of Working Effectively With Legacy Code and Large-Scale C++ Software Design, and do the work piecemeal.
From you question it is not too clear what the problem is - looking at the title (Using non-abstract class as base) I can tell you that using an abstract class (non pure virtual - when you talk about interfaces in C++ I am assuming pure virtual abstract classes) as base makes sense only if there is common functionality you can share between subclasses - meaning that a number of classes extend the same abstract class inheriting the common implementation. If that's not the case (and you're pretty confident it's never gonna happen) then it doesn't make sense to use an abstract class.
If you can extract out some of the functionality in you big class in such a way that leads to (even potential) code reuse then it could make sense - otherwise I wouldn't see the point.
I am working on a collection of classes used for video playback and recording. I have one main class which acts like the public interface, with methods like play(), stop(), pause(), record() etc... Then I have workhorse classes which do the video decoding and video encoding.
I just learned about the existence of nested classes in C++, and I'm curious to know what programmers think about using them. I am a little wary and not really sure what the benefits/drawbacks are, but they seem (according to the book I'm reading) to be used in cases such as mine.
The book suggests that in a scenario like mine, a good solution would be to nest the workhorse classes inside the interface class, so there are no separate files for classes the client is not meant to use, and to avoid any possible naming conflicts? I don't know about these justifications. Nested classes are a new concept to me. Just want to see what programmers think about the issue.
I would be a bit reluctant to use nested classes here. What if you created an abstract base class for a "multimedia driver" to handle the back-end stuff (workhorse), and a separate class for the front-end work? The front-end class could take a pointer/reference to an implemented driver class (for the appropriate media type and situation) and perform the abstract operations on the workhorse structure.
My philosophy would be to go ahead and make both structures accessible to the client in a polished way, just under the assumption they would be used in tandem.
I would reference something like a QTextDocument in Qt. You provide a direct interface to the bare metal data handling, but pass the authority along to an object like a QTextEdit to do the manipulation.
You would use a nested class to create a (small) helper class that's required to implement the main class. Or for example, to define an interface (a class with abstract methods).
In this case, the main disadvantage of nested classes is that this makes it harder to re-use them. Perhaps you'd like to use your VideoDecoder class in another project. If you make it a nested class of VideoPlayer, you can't do this in an elegant way.
Instead, put the other classes in separate .h/.cpp files, which you can then use in your VideoPlayer class. The client of VideoPlayer now only needs to include the file that declares VideoPlayer, and still doesn't need to know about how you implemented it.
One way of deciding whether or not to use nested classes is to think whether or not this class plays a supporting role or it's own part.
If it exists solely for the purpose of helping another class then I generally make it a nested class. There are a whole load of caveats to that, some of which seem contradictory but it all comes down to experience and gut-feeling.
sounds like a case where you could use the strategy pattern
Sometimes it's appropriate to hide the implementation classes from the user -- in these cases it's better to put them in an foo_internal.h than inside the public class definition. That way, readers of your foo.h will not see what you'd prefer they not be troubled with, but you can still write tests against each of the concrete implementations of your interface.
We hit an issue with a semi-old Sun C++ compiler and visibility of nested classes which behavior changed in the standard. This is not a reason to not do your nested class, of course, just something to be aware of if you plan on compiling your software on lots of platforms including old compilers.
Well, if you use pointers to your workhorse classes in your Interface class and don't expose them as parameters or return types in your interface methods, you will not need to include the definitions for those work horses in your interface header file (you just forward declare them instead). That way, users of your interface will not need to know about the classes in the background.
You definitely don't need to nest classes for this. In fact, separate class files will actually make your code a lot more readable and easier to manage as your project grows. it will also help you later on if you need to subclass (say for different content/codec types).
Here's more information on the PIMPL pattern (section 3.1.1).
You should use an inner class only when you cannot implement it as a separate class using the would-be outer class' public interface. Inner classes increase the size, complexity, and responsibility of a class so they should be used sparingly.
Your encoder/decoder class sounds like it better fits the Strategy Pattern
One reason to avoid nested classes is if you ever intend to wrap the code with swig (http://www.swig.org) for use with other languages. Swig currently has problems with nested classes, so interfacing with libraries that expose any nested classes becomes a real pain.
Another thing to keep in mind is whether you ever envision different implementations of your work functions (such as decoding and encoding). In that case, you would definitely want an abstract base class with different concrete classes which implement the functions. It would not really be appropriate to nest a separate subclass for each type of implementation.