References and calls between files/classes

Greetings.

I would like to know how to "pass" references between classes.
Let me write in pseudocode for simplicity sake.

1
2
3
4
5
6
7
8
9
10
11
//this is typeThird.cpp
#include "classFirst.h"
#include "classSecond.h"

Class Third
{
   void Third::methodThatUsesClassA (
   
   //if I want to create a variable of class "A" to save the contents of a file to it, how do I do it?
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class A
{
public:
	void SaveContents()
	{
		// save contents of a file.

	}
};

class Third
{

public:
	void methodThatUsesClassA(A& yourAObject)
	{
		// do stuff with yourAObject
		yourAObject.SaveContents();
	}

	//if I want to create a variable of class "A" to save the contents of a file to it, how do I do it?
};


int main()
{
	Third myclass;
	A myAclass;

	myclass.methodThatUsesClassA(myAclass);
        return 0;
}


or pass by const ref if you arent going to change the object.
Last edited on
Is there no way to create the object within the method in which I want to use it instead of the main?
Last edited on
Let me explain myself better.

I had one main, and three .h/.cpp each for a class of their own ("A, "B", "C").
The classes have a relation of dependency between them.

The main created variables of each class and called for some (i.e. five) methods/functions of every class.

What I need to do is to create a new class "D" with the five sub-routines from the main. Each must do just one thing and they all use the classes "A, "B", and "C".
This way the main would only call for the five methods of class "D".

Is it possible?
Hopefully it's clearer now?
Last edited on
Sure, you can create an instance of A inside methodThatUsesClassA.

It all depends on what the life span of the instance is intended to be. Creating it inside methodThatUsesClassA assumes that it does not need to exist outside that function.
Yes, that is the intention. How can I do it, anon?
Adapting mutexe's example:
1
2
3
4
5
6
7
8
9
10
class Third
{  A         a1;   // 1)  As a member of the class
public:
    void methodThatUsesClassA ()
    {  A a2;  // 2) As a local variable inside the function
        a1.SaveContents();  // Save the class member
        // or 
        a2.SaveContents();  // Save the local variable
    }
};

Last edited on
Still doesn't do what I need :(

Let's see

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//this is one of the five sub-routines of the only class the main contains
void TP2::cargarRecorridos(){

    //class declared in another file with variable of the type
    CargadorDeRecorridos cargadorRecorridos;
    string archivoDeCaminos="Caminos.txt";

    /* "recorridos" is a list, "obtener recorridos" gets a list from the file "archivo de caminos*/
    recorridos = cargadorRecorridos.obtenerRecorridos(archivoDeCaminos);

    //another class declared in another file, receives the list
    //just noticed the error may be here - how can I know I'm passing the list correctly?
    Caminante unCaminante(recorridos);

    unCaminante.logisticaDelRecorrido(recorridos);

//this is the constructor for "caminante". "recorridos" must come from the
//method declared above so that "caminante" can use it.

Caminante::Caminante(Lista<Lista<string>*>* recorridos){
    this->recorridos = recorridos;
    this->unaMochila = new Mochila();
    this->caminos = new Lista<Camino*>;
    this->atajo = new Camino;

}
> The classes have a relation of dependency between them.
> Each must do just one thing and they all use the classes "A, "B", and "C".
¿do you know the difference between a class and an object?

> this->unaMochila = new Mochila();
¿why a pointer?

> just noticed the error may be here
¿what error?

> how can I know I'm passing the list correctly?
- it compiles
- the pointer refer to a valid list
- the pointer refer to the list that you should pass
I don't really get your question
Topic archived. No new replies allowed.