Link to home
Start Free TrialLog in
Avatar of Diceman_01
Diceman_01

asked on

Classes as arguments

When passing a class as a parameter to another class' method, is there any thing I should bear in mind?   Maybe some overloading of the = operator, or something else?

I am getting a "Debug Assertion Failed" when the method is called.   I commented out the entire inside of the method, so the code now looks something like this:

======CODE=========
class A{
public:
    contructor();
    destructor();
    someMethods();
private:
    int someData;
    char someMoreData;
};

class B{
public:
    constructor();
    destructor();
    void brokenMethod(A instanceOfA);
private:
    int yetMoreData;
}

void B::brokenMethod(A instanceOfA)
{
    // ALL COMMENTED OUT
    cout << "Returning..." << endl;
}

int main(int argc, char** argv)
{
    A A;
    B B;
    cout << "Calling brokenMethod." << endl;
    B.brokenMethod(A);
    cout << "Returned."

    return 0;
}

======OUTPUT=========
The output looks like this:
Calling brokenMethod.
Returning...

======ERROR=========
Everything complies fine.   During runtime I get:
Debug Assertion Failed!
Program: <Path to my program>
File: dbgheap.c
Line: 1011
Expression: _CrtIsValidHeapPointer(pUserData)

As always, any help is super duper appreciated.

pdm
Avatar of Axter
Axter
Flag of United States of America image

>>When passing a class as a parameter to another class' method, is there any thing I should bear in mind?

You should prefer passing by reference instead of passing by value.
Example:

void brokenMethod(A &instanceOfA);//In your class header


void B::brokenMethod(A &instanceOfA)
{
   // ALL COMMENTED OUT
   cout << "Returning..." << endl;
}
ASKER CERTIFIED SOLUTION
Avatar of Axter
Axter
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Diceman_01
Diceman_01

ASKER

I am a dumbass.   The problem was in the destructor code for the class being used as the parameter.  

I wouldn't have seen it without asking this question.

Thanks for the input.   Points are split.