How can I resolve this matter with the same order of classes?

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
33
34
#include <iostream>
using namespace std;
class A;
class B;
class A{
	public:
		A(): b(5){
			temp=2;
		}
		int get_temp(){
			return temp;
		}
	private:
		int temp;
		B b;
};
class B{
	public:
		B(int x){
			i = x;
		}
		int get_i(){
			return i;
		}
		void ii(int x){
			i = x;
		}
	private:
		int i;
};
int main(){
	A a;
	cout<<a.get_temp();
}
Last edited on
Anyone..?
Any ......................... idea? :P
1.) It's bad practice to include multiple classes into one file.
2.) What is this used for?
3.) What is your logic behind wanting to do it specifically this way?
4.) I'm tired. Good night.
A has a B as member so B needs to be defined before A is defined.
How can I resolve this matter with the same order of classes?

As Peter has said, as class A has a member of class B then the order of definition must be reversed.

If you do for some reason require absolutely that class A is defined before class B in your file, then you can use a pointer to class B in class A instead. But the methods of class A that use B will still need to be defined after class B is defined.

(The reasons that the definition of a class member which is another class must be know beforehand is that the compiler needs to know its size and layout so it can work out the overall layout of the class you're defining. If you use a pointer to the other class instead this is not a problem as the size of a pointer is fixed for a given architecture.)

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
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
using namespace std;
class A;
class B;
class A{
    public:
        A();  // implementation must come after B is defined
        ~A(); // now need destructor
        int get_temp(){
            return temp;
        }
        int get_b_result(); // new method that uses B
    private:
        int temp;
        B* pb; // now a pointer to class B
};
class B{
    public:
        B(int x) : i(x) { // prefer to use init list
            //i = x;
        }
        int get_i(){
            return i;
        }
        void ii(int x){
            i = x;
        }
    private:
        int i;
};
A::A(): temp(2), pb(new B(5)) { // prefer to use init list (note B now new-ed)
    //temp=2;
}
A::~A() {
    delete pb;
}
int A::get_b_result() {
    return pb->get_i();
}
int main(){
    A a;
    cout<<"temp     = "<<a.get_temp()<<"\n";
    cout<<"b_result = "<<a.get_b_result()<<"\n";
    return 0;
}


Andy

PS Regarding

1.) It's bad practice to include multiple classes into one file.

That's a new one on me!

Last edited on
It's bad practice to include multiple classes into one file

I think this only applies if the classes are used for very different things (could be classed into modules) or if you've got a really big project you might put one class per header file to reduce compile times as if you edit one class only the one needs to be recompiled, but I'm not sure about that.
@shadowmouse @andywestken

That was simply what I was taught. I may be wrong with the matter seeing how the other two that responded never even heard of such a system.

Like every other programming style, it is just a formatting system to help with organization.

P.S.
I've probably been on YouTube for too long; I actually thought he was a troll when I read is reply
Anyone..?
Any ......................... idea? :P

or just some guy that was asking that just to post a question. That's why I added
4.) I'm tired. Good night.


@Zeeshan Ali
If you were serious, then I apologize.
Topic archived. No new replies allowed.