Jump to content

Passing a class variable to another class

- - - - -

  • Please log in to reply
1 reply to this topic

#1
lor

lor

    Programming Goddess

  • Members
  • PipPipPipPipPipPipPip
  • 884 posts
Hey,

Okay so I have a class called Questions and another called Responses. They're in seperate files. I have 3 vector variables of type Questions called beginnerQuestions, intermediateQuestions and advancedQuestions which I'm trying to pass to a method (goToNextQuestion) within Responses but it doesn't know what type Questions is i.e. it's undefined.

Within Questions class:

r.goToNextQuestion(questionBox, a, b, c, d, begCount, beginnerQuestions,
            randomAnswers, intermediateQuestions, intCount, advancedQuestions,
            advCount);     //call goToNextQuestion method in Responses class
Within Responses class:

void goToNextQuestion(TMemo *questionBox, TButton *a, TButton *b,
            TButton *c, TButton *d, int begCount, vector<Questions> beginnerQuestions,
            vector<wchar_t*> randomAnswers, vector<Questions> intermediateQuestions,
            int intCount, vector<Questions> advancedQuestions, int advCount)
        {
            begCount++;
            if (begCount <= 4)
            {
                questionBox->Text = beginnerQuestions[begCount].getQuestion();
                randomAnswers = Questions::randomizeAnswers(beginnerQuestions, begCount);
            }
            else if (begCount <= 9)
            {
                questionBox->Text = intermediateQuestions[intCount].getQuestion();
                randomAnswers = Questions::randomizeAnswers(intermediateQuestions, intCount);
                intCount++;
            }
            else if (begCount <= 14)
            {
                questionBox->Text = advancedQuestions[advCount].getQuestion();
                randomAnswers = Questions::randomizeAnswers(advancedQuestions, advCount);
                advCount++;
            }
            else if (begCount == 15)
            {
                Application->MessageBox(L"You won $1,000,000!",
                        L"Congratulations!", MB_OK);
                Application->Terminate();
            }

            a->Caption = randomAnswers[0];
            b->Caption = randomAnswers[1];
            c->Caption = randomAnswers[2];
            d->Caption = randomAnswers[3];
        }

How would I allow class Responses to know the type 'Questions'?

Any questions - please ask.

Also, is passing a bunch of variables bad? I mean, not bad, but does it really slow down the performance of the application? To me it seems like stuffing someone's mouth with a bag of marshmellows and trying to eat them all at once.


#2
fayyazlodhi

fayyazlodhi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 403 posts
One way to do that is, create a header file containing interface of Questions class i.e. may be questions.h

and include that header file in Response class's code. You might need to add a forward declaration like


class Questions;

// Response class API's go here


Passing many variable is not really bad if it suits your particular situation. Does it affect performance or not. In some cases may be YES.
When you pass a lot of variables to functions, they have copies created in stack. So they eat up more memory every time they are created and destroyed and consume more cycles when compared to have a global place where variables are present, created only once and accessed by the parts of program who need them.

But in your above situation it looks okay to me as long as vector<Questions> is passed by reference and not by value. I kind of no longer remember if this is the case.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users