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(pUs erData)
As always, any help is super duper appreciated.
pdm
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(pUs
As always, any help is super duper appreciated.
pdm
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
I wouldn't have seen it without asking this question.
Thanks for the input. Points are split.
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;
}