Subclass Object in Baseclass/Superclass

closed account (DEhqDjzh)
What I am trying to do is:
Base.h
1
2
3
4
5
6
7
#include "Sub.h"
class Base
{

Sub subobject
int x;
}

Sub.h
1
2
3
4
5
#include Base // to acces x from .cpp file
class Sub: public Base
{
void changevar();
}

Sub.cpp
1
2
3
4
5
6
#include "Sub.h"
// I tried to include base in here but that did not work either
void Sub::changevar()
{
x++;
}

But I keep getting undefines base class and undefined undeclared x error.
How can I solve this?
Last edited on
can't, your object size would be infinite
I wasn't sure if you were concerned with "functionality" or the semantics of the C/C++ language. You can have a pointer. This may not help you, but at least it would not hinder you either.

I just changed your object to a pointer and used a reference as a kind of "proxy" for an object. And I initialized the variable x to 0 and put in some ifdef checking, along with a method CreateSubObject and a shorter method Init() to initialize the Base class with the object. And I put in some constructors and/or destructors, along with some code to illustrate the two different copies of Sub (as illustrated by the different values of x printed by the printf). The commented out lines are ideas I tried that didn't work. Also, you could merge the GetSubObject and Init into one line more or less.

This would functionally do what you want but it's not an object within Base - it's a pointer to an object.

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Sub.cpp
#include "Sub.h"
#include "stdio.h" // for printf
// I tried to include base in here but that did not work either
void Sub::changevar()
{
	x++;
	printf("x = %d\n", x);
}

Sub &Base::GetSubObject() { return *psubobject; }
Base::Base()
{
	//psubobject = new Sub;
	x = 0;	
}
Sub *Base::CreateSubObject()
{
	return (psubobject = new Sub);
}

Base::~Base()
{
	delete psubobject;
}

int main()
{
	Sub b;
	b.Init();
	printf("x = %d\n", b.x);
	
	// Test of GetSubObject and changevar ...
	Sub &rsub = b.GetSubObject();
	rsub.changevar();
	rsub.changevar();
	
	b.changevar();
	
	return 0;
}

// Base.h
#ifndef _BASE_H_
#define _BASE_H_

class Sub;
#include "Sub.h"

class Base
{
public:
	Base();
	~Base();
	Sub *CreateSubObject();
	Sub *Init() { CreateSubObject(); }
	//Sub subobject;
	Sub *psubobject;
	Sub &GetSubObject();
	int x;
};
#endif // _BASE_H_

// Sub.h
#ifndef _SUB_H_
#define _SUB_H_
#include "Base.h"

class Sub: public Base
{
public:
	void changevar();
};
#endif // _SUB_H_ 


Compile and run of the program:

# g++ Sub.cpp
# ./a.out
x = 0
x = 1
x = 2
x = 1


Perhaps there is a way to do more than this with templates?
Last edited on
Hi,

The reason there is a problem in the OP, is because of circular includes:

https://stackoverflow.com/questions/17865286/c-circular-include
Topic archived. No new replies allowed.