Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pass the Class Please

Status
Not open for further replies.

Gorecki

Programmer
May 14, 1999
40
US
Hello,
I'm having trouble passing a class object that requires a single parameter for the constructor.

The result I want being, inside another class, pass the already initialized object's pointer to another variable.

eg.
//FirstClass Constructor
FirstClass::FirstClass(char* name)
{
// Start with file not open.
fOpen = false;

// Store the file name.
strcpy(fName, name);
}


//Create FirstClass
FirstClass * FC = new FirstClass("testfile.dat");


//Second Class Constructor
SecondClass::SecondClass(FirstClass* theFirst)
{
FirstClass * dupFirst = theFirst;
}

//Create SecondClass
SecondClass * SC = new SecondClass(FC);

It doesn't appear 'dupFirst' is getting assigned the values from 'theFirst'. Is it because of the missing constructor parameter from FirstClass?

Any help would be appreciated.

Brian
 
Hi..

As is seems to me, your dupFirst variable is a local variable (inside the constructor) and not a member of your SecondClass class - so, whenever you exit from that constructor, it's value will be gone ....

does it help ?

Yaron.
 
It does. Couple hours too late, but better then never. :)

I had a couple of issues. Yes the 'dupFirst' was being treated as a local. I made it global to the class, the had to make a 'copy constructor' in the FirstClass.

Works like a charm now. Amazing how something so stupid can drive us nuts!!! :)

B
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top