TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: Neil Heller
from: Darin McBride
date: 2003-01-16 14:28:28
subject: Declaring a pointer to a

Hello Neil!

Replying to a message of Neil Heller to Darin McBride:

 DM>> I used to do this all the time.  It should work.

 JB>>> I find having two different compilers handy as I can get a
 JB>>> second opinion :)

 NH>> An excellent idea.

 DM>> So the obvious question is: what compiler are you using now?

 NH> Of whom are you asking that question?  I'm using MSVC 5.

MSVC 5 should be okay by now, I'd think.  Maybe if you were using version
1.3 or something I could see some issues, but even that ... I was doing
this stuff on MSVC 1.41 without issues.  At least, not these issues :-)

 NH> Ok, ok.  I did the following test program and compiles and runs 
 NH> correctly.  Since I backed-up from an earlier version of my program
 NH> I'll  use this as a template for an update.  In the meantime, can you
 NH> see any  way I can clean this up?


 NH> File:  a.h

 NH> #ifndef PartA
 NH> #define PartA

 NH> class ClassA
 NH> {
 NH> private:
 NH>    int unused;

 NH> public:
 NH>    void DoitA();
 NH> };

 NH> typedef class ClassA * ClassAptr;

I generally don't create these typedefs unless I'm trying to hide the fact
that ClassAptr is actually a pointer (i.e., I may change it to something
else in the future - say a reference-counting object that holds a ClassA
object).

That said, nothing fundamentally wrong with it.

 NH> #endif


 NH> File:  a.cpp

 NH> include "ClassA.h"
 NH> #include "ClassB.h"

 NH> void  ClassA::DoitA()
 NH> {
 NH>    ClassB cb(this);
 NH> }


 NH> File:  b.h

 NH> #ifndef PartB
 NH> #define PartB

 NH> #include "ClassA.h"

 NH> class ClassB
 NH> {
 NH> private:
 NH>    ClassAptr abc;

 NH> public:
 NH>    ClassB();
 NH>    ClassB(ClassAptr);
 NH> };

 NH> #endif


 NH> File:  b.cpp

 NH> #include "ClassA.h"
 NH> #include "ClassB.h"

 NH> ClassB::ClassB()
 NH> {
 NH> }


 NH> ClassB::ClassB( ClassAptr aaa )
 NH> {
 NH>    abc = aaa;
 NH> }

Again, nothing fundamentally wrong, but try:

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

A couple reasons: a) there are some anal-retentives on c.l.c++ who will
flame you for your style because b) this style is faster (fewer runtime
cycles).  The difference is that yours initialises abc with the default
constructor (which, for a pointer, is a no-op, really) and then uses the
assignment operator to copy the pointer.  Mine will use the constructor
that takes ClassAptr as its parameter - initialise in a single step.  In
practice, with built-in types (pointers, int, char, etc), there's no
difference, but you'll still get flames :->

 NH> File:  test.cpp

 NH> #include "ClassA.h"

 NH> int  main()
 NH> {
 NH>    ClassA *  ac = new ClassA();
 NH>    ac->DoitA();
 NH>    delete ac;
 NH>    return 0;
 NH> }

I don't see any fundamental differences between this (which compiles) and
the original (which doesn't).  I would then have to make the awesome leap
of logic that the problem is rooted elsewhere ;->

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™.