Here's what I want to achieve: I'm building a class for controlling step motors. In that class I want to have a method called "init". In that method I'd like to create an object of class Tone, which I'll need to control a motor. BUT I want to have seperate Tone class object for every object of my class that I create. How can I do it?

So, for exmaple, if I have:

myClass newmotor1;

After executing these two lines, I'd like to have my object newmotor1 (which I have, because I just created it) AND an object of class Tone, which I'll use.

  • Why do you want to create it in the init function? Why not just have one in the class already? – Majenko Jul 16 '16 at 18:35
  • But I want to have seperate Tone objects for each object of my class – Loreno Jul 16 '16 at 18:39
  • And? What difference does that make? – Majenko Jul 16 '16 at 18:40
  • To be honest, I'm not sure what you mean. I'm actually new to classes, objects and all that stuff. Could you explan? – Loreno Jul 16 '16 at 18:41
  • You use a class just like any other variable. Just use it in your class like any other variable. – Majenko Jul 16 '16 at 18:41

You don't need to "create" anything. Just have an object in your class:

class Tone {
        void dosomething() {
            // Whatever is in here

class MyMotor {
        Tone tone;
        void init() {
            // Nothing much else to do in here now.

You then:

MyMotor newmotor1;

And you can access:

  • That's a perfect solution. Thanks Majenko! – Loreno Jul 16 '16 at 18:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.