1. Singleton
You restrict the number of instances because the constructor will be private meaning only static methods can create instances of that class (there are other dirty tricks actually to accomplish that but let's not get carried away).
Creating a class that will have only 2 or 3 instances is perfectly feasible. You should be using singleton whenever you feel the necessity to have only one instance of that class in the entire system. That usually happens to classes that have a 'manager' behavior.
If you want to learn more about Singletons you can start in Wikipedia and particularly for C++ in this post.
There is definitely some good and bad things about this pattern but this discussion belongs somewhere else.
2. Abstract Classes
Yes, that's right. Only a single virtual method will mark the class as abstract.
You'll be using those kind of classes when you have a bigger class hierarchy in which the top classes should not really be instantiated.
Let's supposed you're defining a Mammal class and then inheriting it to Dog and Cat. If you think about it, there's no much sense to have a pure instance of a Mammal since you first need to know what kind of Mammal it really is.
Potentially there's a method called MakeSound() that will only make sense in the inherited classes but there's no common sound that all mammals can make (It's just an example not trying to make a case for mammal's sounds here).
So that means Mammal should be an abstract class since it will have some common behavior implemented to all mammals but it's not really supposed to be instantiated. That's the basic concept behind abstract classes but there's definitely more to it that you should learn.
3. Interfaces
There are no pure interfaces in C++ in the same sense that you have in Java or C#. The only way to create one is by having a pure abstract class which mimics most of the behavior you want from an interface.
Basically the behavior you're looking for is to define a contract where other objects can interact with without caring about the underlying implementation. When you make a class purely abstract it mean all implementation belongs somewhere else so the purpose of that class is only about the contract it defines. This is a very powerful concept in OO and you should definitely look more into it.
You can read about the interface specification for C# in MSDN to have a better idea:
http://msdn.microsoft.com/en-us/library/ms173156.aspx
C++ will provide the same kind of behavior by having a pure abstract class.
One way is to add a public Attach()
method and also a protected AttachWithoutReciprocating()
method to each class. Make A
and B
mutual friends so that their Attach()
methods can call the other's AttachWithoutReciprocating()
:
A::Attach(B &b) {
Bs.push_back(&b);
b.AttachWithoutReciprocating(*this);
}
A::AttachWithoutReciprocating(B &b) {
Bs.push_back(&b);
}
If you implement similar methods for B
, you won't have to remember which class to call Attach()
on.
I'm sure you could wrap that behavior up in a MutuallyAttachable
class that both A
and B
inherit from, thus avoiding repeating yourself and scoring bonus points on Judgement Day. But even the unsophisticated implement-it-in-both-places approach will get the job done.
Best Answer
C and C++ are different in this respect.
C 2011 Online Standard
In short, an empty parameter list in a function declaration indicates that the function takes an unspecified number of parameters, while an empty parameter list in a function definition indicates that the function takes no parameters.
As far as C is concerned, you should never use an empty identifier list in a function declaration or definition. If a function is not meant to take any parameters, specify that by using
void
in the parameter list.Online C++ standard
In the case of C++, an empty parameter list in either a declaration or a definition indicates that the function takes no arguments, and is equivalent to using a parameter list of
void
.