1

I'm trying to control a Dynamixel motor using the C++ SDK and I would like to do this from a class if possible. I need two pointers to an object and one other object to be class member variables. I found How can I initialize C++ object member variables in the constructor? but while it discusses both pointers to objects and objects themselves I need a combination. The three members I need look like this (taken from the sample code):

dynamixel::PortHandler *portHandler = dynamixel::PortHandler::getPortHandler(DEVICENAME);

dynamixel::PacketHandler *packetHandler = dynamixel::PacketHandler::getPacketHandler(PROTOCOL_VERSION);

dynamixel::GroupSyncWrite groupSyncWrite(portHandler, packetHandler, ADDR_MX_GOAL_POSITION, LEN_MX_GOAL_POSITION);

The issue is the GroupSyncWrite object depends on the pointers above it and must be declared after the first two are, so something like this:

Foo::Foo() : groupSyncWrite(...)
{
    porthandler = dynamixel::PortHandler::getPortHandler(DEVICENAME);
    packetHandler = dynamixel::PacketHandler::getPacketHandler(PROTOCOL_VERSION);
}

doesn't seem to work since the groupSyncWrite constructor takes porthandler and packethandler as parameters.

If anyone knows how to do this or if it's not possible to implement it like this I would appreciate any help.

EDIT:

The question in a nutshell is how to initialize two object pointers in a constructor prior to an object initialization in the same constructor.

There is an object foo that requires two pointers to objects as parameters, bar *one, and baz *two. The object and pointers are all members of my class and need to be initialized in the constructor of the class. The problem was I did not know how to initialize the pointers in the format

Class::Class() : one() {}

such as can be done with non-pointer objects. @Matthieu's answer worked perfectly.

Ben H
  • 113
  • 1
  • 5
  • I'm not quite following what you are doing and where the problem lies. I think you need to reduce your problem a bit to a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). A `Foo` and a `Bar` class should do just fine. It should not be much more than [c++ callback this pointer](https://www.google.com/search?q=c%2B%2B+callback+this+pointer+site:stackoverflow.com), but I can't say for sure. – jww Nov 01 '18 at 22:08
  • The entirety of my problem was how to initialize the two object pointers in the constructor prior to the groupSyncWrite object initialization which depended on the pointers. [@Matthieu](https://stackoverflow.com/users/2266772/matthieu-brucher)'s answer worked perfectly. In retrospect the question is somewhat vague so I've updated it to more accurately reflect what I was looking for. – Ben H Nov 02 '18 at 23:18

1 Answers1

1

Initialize them properly and in order in the initialization list from the constructor:

Foo::Foo()
 : porthandler(dynamixel::PortHandler::getPortHandler(DEVICENAME)),
 packetHandler(dynamixel::PacketHandler::getPacketHandler(PROTOCOL_VERSION)),
 groupSyncWrite(porthandler, packetHandler)
{
}

Make sure you have the proper order when declaring the member variables of your class (porthandler, then packetHandler and finally groupSyncWrite).

Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62