About create an instance of a external class.

I dont know how to do this.

I have my classA with its .h and .cpp file.
I have my classB with its .h and .cpp file.

ok, into myclassB, I want to have ClassA var-element-instance-object
(I keep on without to know how to name it!!).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
classB.h
 #include "classA.h"

 private :
 classB my_instance_of_B of classB
 ....

Class B.cpp

classB my_instance_of_B   //<<<< 

classB(){}
void a_function() 
{
my_instance_of_B.a_method();
}

OK.
If I dont write the line 'classB my_instance_of_B //<<<< '
when compile I have "classB not declared'.

I dont understand this.
If I use a #include "string.h" I simply create my strings...

Can anyone explain me or give me a link?
Thanks
Last edited on
I reread your post multiple times and I have still no idea what you're asking.
You should start by giving a real code example (not pseudo-code) that illustrates your problem.
Excuse me (other time that I don't explain it well.)

I have a problem with a private - public instance of a custom class.
Depends on how I write the code I have errors or no.

I can write the instantiation of the class :

1.- Inside the .h file into the public section
2.- Inside the .h file into the private section

- In both cases, at cpp file.... where It is the instance properly created?
- Have I to write it at the begining of the code? at the constructor?
- And If I want to re-instance inside a method, How must I write the code ? Using 'this' ?

Thanks.

The constructor is called when a new instance of your object is created.
You don't explicitly need to write "this" when accessing member variables in member functions.
Excuse me again.
I dont talk about the constructor of the object instantiated.

I'm going to explain my doubts again.

I have my ClassA with its .h and .cpp file.
I have my ClassB with its .h and .cpp file

I want to have an object of classB inside classA.

ok. The code I have to write at ClassA.h is :
1.- #include classB.h
2.- ClasssB class_b_to_create; (public or private)

ok. now at ClassA.cpp :

Have I to write again ClasssA class_a_to_create at the beggining of the file? (just before the constructor). I dont know if it is neccesary.
In this case, when I run the program which is the place where the objetc of classb created?
And by last, How can I reset the classb created? I want to have the posibility of to do it inside a method : for example

1
2
3
MyclassA::reset_class_b_created()
{  ClasssB class_b_to_create;
}
But this create a private class_b_to_create with MyclassA::reset_class_b scope.

I hope now you understand me.
Thanks














Last edited on
If you want to have an instance of classB inside classA, you should be writing the following:

1
2
3
4
5
6
#include "classB.h"

class classA
{
  classB b;
};


You don't have to specify anything in the .cpp file.
The creation of the classB instance is done between the call to the constructor and the point the constructor body is executed.
You can call a certain classB constructor using the initializer list:
1
2
3
4
classA::classA() : b(some,parameters)
{
  //b is already constructed at this point
}


Otherwise, the default constructor is used.
The classB instance is bound to the lifetime of the classA instance.
If, for some reason, you need to destroy the classB instance before the classA instance is destroyed, you can use pointers:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class classA
{
  public:
    classA();
    ~classA();

  private:
    classB* b;
};

classA::classA() : b(new classB)
{
}

classA::~classA()
{
  delete b;
}

//and when you want to delete the instance somewhere else:
delete b;
b=0;


It would be cleaner if you used scoped_ptr or unique_ptr instead of raw pointers, though.
Last edited on
Thanks AThar ( and thanks for your patience...)
Your explanation is good.

By last, if I understand, if I dont create the object using pointers is not posible to delete it or to re-instance it , isn't it ?
Last edited on
Topic archived. No new replies allowed.