0

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;
memotor1.init();

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.

asked Jul 16, 2016 at 18:32
8
  • Why do you want to create it in the init function? Why not just have one in the class already? Commented Jul 16, 2016 at 18:35
  • But I want to have seperate Tone objects for each object of my class Commented Jul 16, 2016 at 18:39
  • And? What difference does that make? Commented Jul 16, 2016 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? Commented Jul 16, 2016 at 18:41
  • You use a class just like any other variable. Just use it in your class like any other variable. Commented Jul 16, 2016 at 18:41

1 Answer 1

0

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

class Tone {
 public:
 void dosomething() {
 // Whatever is in here
 }
};
class MyMotor {
 public:
 Tone tone;
 void init() {
 // Nothing much else to do in here now.
 }
};

You then:

MyMotor newmotor1;

And you can access:

newmotor1.tone.dosomething();
answered Jul 16, 2016 at 18:43
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.