A "new" instance

S

seesaw

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?

Thanks!
 
T

Thomas Wintschel

seesaw said:
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.

Is the constructor private for some reason? If it is, only an A can
construct another A and you would need to write:
A *pA = A::newA();
in order to create one. Such static methods can also show up in other
places, albeit with more general names (see factory pattern).
2. newA as a member method of class A IS returning a "new" instance of A
while it is still defining class A.

Don't confuse compile time and run time. The class will be compiled
before newA is ever executed and it is the compilers job to ensure that
it works correctly.
 
J

John Carson

seesaw said:
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.

A static member function is like a free function in that it exists
independently of any individual object of the class. You can call it with

A::newA()

without every having declared an object of type A. By contrast, with a non
static member function called foo, you could NOT call

A::foo()

Instead, you would have to do something like:

A a;
a.foo();

Because a static member function is not bound to an individual class object,
it does not have a "this" pointer that allows it to access member data of an
object.

On the other hand, it is different from a free function in that

1. It belongs to the "namespace" of the class, so that you must call
A::newA() rather than just newA(),
2. It automatically has access rights to an object's private and protected
members in the same way that a free function would have if it was declared a
friend of the class (in both cases, these access rights can only be utilised
if an object of the class is made available to the function in some way,
e.g., by passing a reference to an object as a function argument or by
declaring an object of the class as a local variable within the function).
2. newA as a member method of class A IS returning a "new" instance
of A while it is still defining class A.

newA doesn't return anything just by being defined. It only returns
something when it is called.
 
J

John Harrison

seesaw said:
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
 
J

jeffc

seesaw said:
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?

There is really no need to do this out of context. Instead, you'd simply
write

A* pA = new A;

rather than

A* pA = A::newA();

But it might be a simplified example for times when you want to control how
objects of your class get created. For example, let's say for some reason
there should never be more than 3 instances of your class existing at any
one time. Then you might make the constructor for A be private (that way no
one can do "new A;".) They must use the function newA() to create an
object. But you check how many have been made, and don't make a new one if
3 have already been made.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,812
Members
47,357
Latest member
sitele8746

Latest Threads

Top