1

In my C++ program, I want to create an object that has properties like width, height, area, etc. I also want to declare methods that use and update this properties.

I want the methods that "set" and "get" the propery "width" listed somehow in a header, namespace, or child class (anyway is possible) named WidthManipulator.

The reason I want to create my structure this way is I want to use "get" name for another method of another class, like HeightManipulator.

But for nested classes I get the "illegal call of non-static member function" error for Rectangle::WidthManipulator::Get(). I also don't want to create Manipulator objects as these classes don't have properties, just methods that are using and updating parent properties... One more thing, I want to use void returns for a good reason of my own.

class Rectangle{ public: int width,height; int area; int widthreturned; class WidthManipulator{ public: void Set(int x){width = x;} void Get(){widthreturned = width}; }; }; 

How can I approach to my problem ? What should be my structure ?

2
  • Just out of curiousity: What is this good reason to use "void returns"? :) Commented Jun 27, 2013 at 15:18
  • well my code is not this, of course. The structure I need is accepting inputs as reference, updating inside, that's why. Commented Jun 27, 2013 at 15:41

2 Answers 2

6

The inner class in C++ is not like it is in Pascal. It's just paled in the "namespace" of the outer class, but no other thing is changed. It sees only its own members, and the instance is unrelated to the outer one.

If you want the relation, you must pass an instance of the outer somehow, say in the constructor. Then you can access members through that pointer or reference.

Sign up to request clarification or add additional context in comments.

2 Comments

OK. I will try declaring WidthManipulator{ Rectangle R; WidthManipulator(Rectangle rect){R= Rect;} }; and keep track of member R. Thanks for the help
you must make it Rectangle &rect or Rectangle* pRect, the way you do it you get a copy of the original!
3

Am not sure why you want to structure your class manipulator this way, but to understand the mechanism of the Inner class consider that for the Inner class to access OUter class members, outer class should declare Inner as a friend:

class Outer { int area; class Inner1; friend class Outer::Inner1; class Inner1 { Outer* parent; public: Inner1(Outer* p) : parent(p) {} void Set(int x){p->area= x;} } inner1; // ... more stuff }; 

If you want to check out with more detail, I recommend you to look at the design pattern example Chapter 11 Vol 2 Thinking in C++

1 Comment

Thanks, totally understood the concept now.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.