473,406 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

A "new" instance

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!
Jul 22 '05 #1
4 4311

"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.
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.
Could someone elaborate the two for my retarded head?

Thanks!

Jul 22 '05 #2
"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.
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.
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #3

"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
Jul 22 '05 #4

"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?


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.
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Paul Auleciems | last post by:
Hi: I'm having trouble using an Object which is created based on the following: Public CarDetail () as Car Where the CLASS "Car" is defined as: Public Class Car
24
by: Rv5 | last post by:
Rookie c++ question, but Ive spent the last 5 years doing Java, where everytime I created an object I used new. In c++ I can create my objects without and its confusing me just a little. I have...
1
by: Sam | last post by:
Hi everyone Could anyone help me understand the usage of the "New" keyword I'm new to VB.Net. 1. Why do we use the "New" keyword on some object type variables such as the myPen of the...
7
by: CMirandaman | last post by:
I have another newbie question I could use some help with. I ran across this code in one of the MS training guides: public class NullTokenVisitor {...} public class Application { public...
51
by: Tony Sinclair | last post by:
I'm just learning C#. I'm writing a program (using Visual C# 2005 on WinXP) to combine several files into one (HKSplit is a popular freeware program that does this, but it requires all input and...
24
by: M O J O | last post by:
Hi, Instead of doing this.... Public Class Form1 Public Shared Sub CreateAndShow() Dim f As New Form1 f.Show() End Sub
37
by: jht5945 | last post by:
For example I wrote a function: function Func() { // do something } we can call it like: var obj = new Func(); // call it as a constructor or var result = Func(); // call it as...
4
by: Ronald S. Cook | last post by:
So most of the time I need to write the following to instantiate an object: Dim cnn As New SqlConnection() But sometimes Intellisense tells me not to include the "new": Dim dst As DataSet ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.