Hi people i need yor help. It must be easy for you but im starting with c++ and i can not figure put how to do this:
I have two classes, each on with the .h and .cpp files and a main class that must call the objects created for those classes. Here is a template:

class1.cpp

int ClassA::function1()
{
...
return a
}

int ClassA::function2()
{
}

classb.cpp

int ClassB::function3()
{
}

int ClassB::function4()
{
}

main.cpp

#include "class1.h"
#include "class2.h"

int main (int argc, char* argv[])
{
    Class1 class1;
    Class2 class2;
    class1.function1();
    class2.function3();
    return 0;
}

So what i need to do is to pass the data returned in class1.function1(); and use it in class2.function3();
For example class1.function1() returns "int a" then i need in class ClassB::function3() write somthing like:

int b = a *2;

Thanks in advance!!

Recommended Answers

All 5 Replies

You can store the return value in a new variable and pass it to the use the second function or you can place the function call from the first function inside the parameter list of the second.

int temp = classA.function1();
classB.function3(temp);

classB.function3(classA.function1());

You can store the return value in a new variable and pass it to the use the second function or you can place the function call from the first function inside the parameter list of the second.

int temp = classA.function1();
classB.function3(temp);

classB.function3(classA.function1());

You will also need to change your function declarations to accept parameters.

int ClassB::function3( int temp )
{
}

Thanks for the replies but that's not the way i need to solve my problem because i need to pass directly the returned value from ClassA.function1() to ClassB.function3(). Thats because ClassB is a template and i can't modify it so i can't instance that class in the main() class. Is there some way to do that?

class A
{
    public:
        int function1()
        {
              return 1; // ...
        }
};
class B
{
    A a;
    public:
        int function1()
        {
             return a.function1();
        }
};

>>>Thats because ClassB is a template and i can't modify it so i can't instance that class in the main() class.

??????? main() isn't a class, it's a function. The only class or user defined type you can't instantiate is an abstract data type (and even that may not be an iron clad rule). Maybe the best thing to do is post relevant code, especially if cikara21's approach doesn't do what you want either.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.