Your code(more like your idea) is actually error free and valid on both C++03 and c++11.
There are 2 errors in your code however. To create an object of type B, you need to see the entire definition of B, and that means the definitions of B and A need to be exchanged.
Secondly, you need to make myfunction
public, or call it from within class A.
To answer your other question.. Most singletons are implemented somewhat like this..
class Singleton{
private:
Singleton(){}
public:
static Singleton& GetInstance(){
static Singleton instance;
return instance;
}
};
This would prevent anybody but the Singleton class (and a friend as you have discovered) from making an instance of the class, so it is easier to enforce the 1 object rule. The Singleton will be accessed as Singleton::GetInstance()
.
This and other possible implementations are shown at Wikipedia.