Proper way to access Objects within objects?

I am attempting to access members of an object that I have created within another object but the proper syntax is eluding me. Or, perhaps the problem is how I am creating the inner class?

This should be all the relevant bits. I have tried many formats for my call to the inner class and keep getting variations on the error following the code. Any help would be appreciated.

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
#include <iostream>
#include <memory>
using namespace std;

class InnerClass   {
	public:
		
		InnerClass() { val = 10; }
		int getval() { return val; }
		
	protected:
		int val;

};

class OuterClass   {
	public:
		OuterClass()  {this-> Inner = 0;}
		InnerClass * Inner;
}; 

int main()    {

	OuterClass * Outer;
	
	std::cout << Outer.Inner.getval()  << std::endl;

return 0;
}


request for member ‘Inner’ in ‘Outer’, which is of non-class type ‘OuterClass*’


I should probably add, that the reason I am creating the classes as empty pointers is that I actually have child classes I am filling them with, however I am fairly certain that part is functioning correctly and my problem is the actual syntax of accessing the nested object members.
Last edited on
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
#include <iostream>
#include <memory>
using namespace std;

class InnerClass
{
public:

	InnerClass()
	{
		val = 10;
	}
	int getval()
	{
		return val;
	}

protected:
	int val;

};

class OuterClass
{
public:
	OuterClass()
	{
		this->Inner = 0;
	}
	InnerClass * Inner;
};

int main()
{

	OuterClass * Outer;

	std::cout << Outer->Inner->getval() << std::endl;

	return 0;
}


when using a pointer to an object you need to use -> point to operator
Last edited on
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
#include <iostream>
#include <memory>
using namespace std;

class InnerClass
{
public:

	InnerClass()
	{
		val = 10;
	}
	int getval()
	{
		return val;
	}

protected:
	int val;

};

class OuterClass: public InnerClass
{

};

int main()
{

	OuterClass * Outer;

	std::cout << Outer->getval() << std::endl;

	return 0;
}


Assigning the outerclass as a child like this is probably better than making the internal pointer to it although it does this behind the scenes anyway.
By the way your protected val is accessible to the derived class in a protected: mode.
make it private: to prevent this.
Last edited on
You haven't created an object of either class in the snippet that you posted, you've only declared pointers.
You haven't created an object of either class in the snippet that you posted, you've only declared pointers.


Yes that's true also, you only created a pointer to a type of class.
Last edited on
Ooops! Missed some relevant creation code. Sorry if that caused any confusion.

However, CodeGoggles was on the spot! I needed more "->"! I had tried using one, but sorta forgot I'd need a second one for the member.

Thanks a ton guys.
> Assigning the outerclass as a child like this is probably better than making the internal pointer to it
http://berniesumption.com/software/inheritance-is-evil-and-must-be-destroyed/
http://www.javaworld.com/article/2073649/core-java/why-extends-is-evil.html

Also, ¿what if you need several `OuterClass' related to the same `InnerClass' object?


Edit: there is no information about the kind of relationship, ¿why did you suggest inheritance?
Last edited on
Edit: there is no information about the kind of relationship, ¿why did you suggest inheritance?


You are right, there was no information that it should be inherited. I sort of got in my head that it was what he was after because of the inner and outer variable names implied to me that he did indeed want to build classes from a base class.

But yes as ne555 stated you will not want this if you are pointing to a single object from many objects.

My apologies.
Last edited on
Topic archived. No new replies allowed.