TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: Neil Heller
from: Darin McBride
date: 2003-01-20 17:32:38
subject: Declaring a pointer to a

Hello Neil!

Replying to a message of Neil Heller to Darin McBride:

 DM>> I do believe that my electronic tax package from last year 
 DM>> had a 16-bit Win3.1 version.  Of course, those are changed 
 DM>> yearly, so I assume that there is still some 16-bit 
 DM>> development going on :-)

 NH> That amazes me. It will be of interest to see how long this practice 
 NH> continues.

I'll be getting the 2002 tax package in the next month or so. 
Unfortunately, it only comes in Windows versions (so far).  Fortunately, my
employer has graciously purchased a copy of VMWare for my use, so I have
WindowsXP installed under Linux.  Great package.  If I remember, I'll look
to see if they still have their 16-bit version.

 DM>>> ClassB::ClassB( ClassAptr aaa ) :
 DM>>>     abc(aaa)
 DM>>> {
 DM>>> }

 NH>> I'm completely unfamiliar with this notation when used in a
 NH>> constructor.   Does this mean that the programmer will
 NH>> ultimately be responsible for  making a constructor
 NH>> specifically for this situation?

 NH> About 2 hours after I had sent this response, I remembered posing an 
 NH> almost identical question to you about 7 years ago.  That question

Not *completely* unfamiliar, are we.  :-)

 NH> dealt  with the base-class initialization list.  Thanks for the
 NH> reminder.   Needless to say, I haven't used the construct at all...
 NH> although I now  can see there is a real reason to do so (speed).

 NH> BTW, is this methodology used often?

It is the recommended way to do things by any advanced C++ programmer. 
That is, advanced programmers recommend it - to anyone, including
beginners.  There are two basic reasons:

 SPEED

As demonstrated, this method can be much faster.

 WORKS IN MORE SITUATIONS

A new demonstration that can confound and annoy beginners.

class Ray2D
{
  public:
  Ray2D(int x, int y);
  // ...
};

Notice: no default constructor.

class Ray3D
{
  public:
  Ray3D(int x, int y, int z_)
    // : ray2d(x, y), z(z_)  // "A"
  {
    // ray2d = Ray2D(x,y); z = z_; // "B"
  }
  // ...
};

Since ray2d has no default constructor, if you uncomment line "B"
by itself, it will not compile - the compiler cannot find the default
constructor for ray2d, so it can't initialise that member!  Line
"A", however, will work fine.

So what does the novice programmer do?  Sacrafice even more speed,
replacing line B with:

p_ray2d = new Ray2D(x,y); z = z_; // "C"

Another memory allocation - and this one is definitely on the heap!  Ugh! 
Compare two uses:

Ray3D r3d(1,2,3);
Ray3D* pr3d = new Ray3D(4,5,6);

The first one would be almost no cycles if you used line "A", and
have all that overhead of a heap allocation with "C".  The
second, would be a single heap allocation with "A", and two with
"C".  Presumably in "C", the reason for the heap
allocation is known by the user (e.g., passing it back to the caller). 
There's no reason to have this object hold a pointer to another object in
either case.

That's not to say that you shouldn't use pointers in your objects - there
are many valid reasons (polymorphism, reference counting, shared objects
with another object, linked lists, pointers to a parent object, etc.). 
Initialisation based on no default constructor should not be one of these
reasons.

Darin
C_PLUSPLUS moderator
dmcbride{at}tower.to.org

---
* Origin: Tanktalus' Tower BBS (1:250/102)
SEEN-BY: 633/267 270
@PATH: 250/102 99 10/345 379/1 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.