Solved

Classes as arguments

Posted on 2004-10-24
209 Views
Last Modified: 2010-04-01
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
0
Question by:Diceman_01
    4 Comments
     
    LVL 30

    Expert Comment

    by:Axter
    >>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
     
    LVL 30

    Accepted Solution

    by:
    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
     
    LVL 10

    Assisted Solution

    by:Sys_Prog
    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
     

    Author Comment

    by:Diceman_01
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
    Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
    The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
    The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

    845 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now