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
Diceman_01Asked:
Who is Participating?
 
AxterCommented:
I notice that your A class has data member someMoreData as a char type.

Are you trying to use this data member as a string?
0
 
AxterCommented:
>>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;
}
0
 
Sys_ProgCommented:
The above code run fine for me. Which compiler do u use. also, are u doing something more than u have shown above.

Amit
0
 
Diceman_01Author Commented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.