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

DM> Yes - pare it down to the smallest compilable version that shows
DM> your  problem.  Or, in your case, the smallest version that seems 
DM> to be syntactically correct - i.e., if you removed the 
DM> line(s) with the errors, it would now compile.  What you 
DM> gave doesn't compile because there is no "this" in your 
DM> .cpp example.  It doesn't even really need to have main() 
DM> since you're not worried about its output.

NH> The situation is that I wish to create the object in a member
NH> function.   Then I need to manipulate the data in an unrelated
NH> class WITHOUT  creating a new object in that unrelated class.
NH> I thought that I could  succeed by passing pointers to the
NH> original class, but...

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?

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


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


File:  a.h

#ifndef PartA
#define PartA

class ClassA
{
private:
   int unused;

public:
   void DoitA();
};

typedef class ClassA * ClassAptr;

#endif


File:  a.cpp

include "ClassA.h"
#include "ClassB.h"

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


File:  b.h

#ifndef PartB
#define PartB

#include "ClassA.h"

class ClassB
{
private:
   ClassAptr abc;

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

#endif


File:  b.cpp

#include "ClassA.h"
#include "ClassB.h"

ClassB::ClassB()
{
}


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



File:  test.cpp

#include "ClassA.h"

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

þ CMPQwk 1.42 999

--- Maximus/2 3.01
* Origin: COMM Port OS/2 juge.com 204.89.247.1 (281) 980-9671 (1:106/2000)
SEEN-BY: 633/267 270
@PATH: 106/2000 1 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™.