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.

2
  • 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. A Foo and a Bar class should do just fine. It should not be much more than c++ callback this pointer, but I can't say for sure. Commented Nov 1, 2018 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'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. Commented Nov 2, 2018 at 23:18

1 Answer 1

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).

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

3 Comments

The order of the list doesn't matter, the order of declaration does. You should add that the OP should have a correct declaration order.
Thanks for the enhancement indeed, it's not always intuitive to do it consistently.
Works perfectly, thank you. I was not aware that you could initialize pointers like that, so that is good to know.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.