Access parent variable

Hello, How can I access to flag from ClassA in the ClassB? Thx, Renaud

class ClassB {
  public:
    ClassB(void);
  private:
};

ClassB::ClassB() {
  /*
  How to access to flag from ClassA here?
  */
}

class ClassA {
  public:
    boolean flag;
    ClassA(void);
    ClassB classB;
  private:
};

ClassA::ClassA() {
}

ClassA classA;

Maybe I am wrong, but this should do it.

classA.flag

And put your code inside code tags. HTML will do funny things with some code.

click the MODIFY button in the upper right of the post window. Highlight all you code. click the "#" CODE TAGS button on the toolbar above just to the left of the QUOTE button. click SAVE (at the bottom). When you post code on the forum, please make a habit of using the code tags "#" button.

The classB instance knows NOTHING of the ClassA instance that contains it. You can NOT, in the ClassB constructor access anything about ClassA.

Now, if ClassB were to derive from ClassA, it would be a different story.

Class A and B have no inheritance connection whatsoever. The title is misleading :)

ClassB is included in ClassA. I should be able within ClassB to reach his parent and reach the flag value.

I should be able within ClassB to reach his parent and reach the flag value.

Bullshit. ClassB knows NOTHING about ClassA. Suppose that classB was an int, instead. Would you be claiming that int should know something about the class that it is used in. Of course not. You're not that stupid. So, get over the idea that because a class is instantiated in another class that the instance knows something about the container.

It's been so long since I worked with Classes that I know I'd need to spend a solid week or more brushing up and knocking the corrosion off my poor disused neural contacts. Perhaps even start with a bottle of 'solvent', sit and soak then dry out first. 8)

Include a new property in class B which is a pointer to type class A. In the class A constructor, initialize the pointer in the contained class B. Note however that you will not be able to access this pointer in the class B constructor. Why? Because an instance of class B must be constructed before an instance of (or in the process of) class A in constructed. Thus at the time the class B constructor is called, the parent class A object doesn't exist yet.

Thanks for the info.

Why not start with Class A and inherit that into Class B? Make all the parts in A public so that B can see them.

Can you suggest me a reference guide about the Arduino language? I need to improve my knowledge about classes.

I am turning around to create a list of classes inside a class. myLib.addButton(10, 10, "btn"); in this method, I want create a new instance of a class button and add it to a list contained in myLib class.

Thx, Renaud

Renaudp: Can you suggest me a reference guide about the Arduino language? I need to improve my knowledge about classes.

It's just C++. Any information about C++ classes will be sufficient for Arduino development.

There are Classes to avoid on most Arduinos and a Class-on-PC practice to avoid as well.

Any that use dynamic allocation/de-allocation as or in their normal operation. Like the Container Classes, like String but there are really many more. Or just making new objects and deleting them on need.

These are things best used/done on computers with lots of memory, the more the better.

You might get away with using/doing these things on Arduino with lots of care but if in doing so you don't learn or think of more suited ways then you're going to find Arduino to be more tight than necessary.

Other than that, Classes offer organizing features to code that can really help to keep it clean and shorter.

ClassB(void);

That's a very retro thing to do with a class declaration.

As GoForSmoke pointed out, you should use inheritance.
There are two methods you can research, dynamic inheritance and static inheritance.

To solve the problem using dynamic inheritance you would make ClassA derived off ClassB. This method is quite simple, but once you start using virtual functions and dynamic binding, your code size and memory usage can grow significantly. However careful use is fine.

class ClassA {
  public:
    boolean flag;
    ClassA(void);
  private:
};

ClassA::ClassA() {

}

class ClassB : public ClassA {
  public:
    ClassB(void);
  private:
};

ClassB::ClassB() {
  this->flag;
}

ClassB classB;

To use static inheritance things start becoming a little more complex, however it can produce a very efficient result. Stick to the dynamic version until you reach an adequate level of C++ knowledge, this is only to show how your solution can be written.

template< typename Derived >
  class ClassB {
    public:
      ClassB(void);
    private:
      Derived& self() { return *static_cast< Derived* >( this ); }
      const Derived& self() const { return *static_cast< const Derived* >( this ); }	
};

template< typename Derived >
  ClassB< Derived >::ClassB() {
  
    this->self().flag;
}

class ClassA : public ClassB< ClassA >{
  public:
    ClassA(void);
	boolean flag;
  private:
};

ClassA::ClassA() {
}

ClassA classA;

You are deriving B from A there. What is the point of a pointer to A inside of A? The B class having been derived from A has no need of that pointer or a B pointer either.

GoForSmoke: You are deriving B from A there.

Are you referring to my post?

One example derives B from A, the other A from B.

GoForSmoke: What is the point of a pointer to A inside of A?

Again whos post are you referring too, There is no pointer to A inside of A in either of my examples.

GoForSmoke: The B class having been derived from A has no need of that pointer or a B pointer either.

Are you posting in the wrong thread??? What you posted is quite confusing. The examples I posted are quite straight forward.

Sorry. It's been too long since I wrote classes.