"seesaw" <se****@turboweb.com> wrote in message
news:PO*********************@bgtnsc05-news.ops.worldnet.att.net...
class A
{
public:
static A* newA() { return new A; }
...
};
In the code, two things not very clear and natural to me:
1. the method newA() is defined as static.
2. newA as a member method of class A IS returning a "new" instance of A
while it is still defining class A.
Could someone elaborate the two for my retarded head?
The code is *almost* identical to this
class A
{
};
A* newA() { return new A; }
The main difference is that in the first case the user says
A* a_ptr = A::newA();
and in the second the user says
A* a_ptr = newA();
Apart from that there is really no essential difference. In the second case
the function newA can only access the public parts of A, which would be a
problem if the A default constructor was not public, but that could easily
be overcome with a friend declaration.
class A
{
friend A* newA();
private:
A();
};
A* newA() { return new A; }
Sometimes code like this is written to restrict the ways in which A objects
can be constructed. Some people prefer the first form, some people prefer
the second, it makes very little difference.
john