在同一个类中调用另一个构造函数 [英] Invoke another constructor in the same class

查看:204
本文介绍了在同一个类中调用另一个构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类有2个公共构造函数,我想调用一个私有构造函数:

I have a class with 2 public constructors, that I want to call a private constructor:

class CDeviceTSGetObservationResponse : public CDeviceServerResponse
{
public:
   /**
    * Public constructor. Used to construct a response containing
    * the required information from the TotalStation device.
    *
    * @param horizontalAngle
    *           The horizontal angle.
    * @param verticalAngle
    *           The vertical angle.
    * @param slopeDistance
    *           The slope distance.
    */
   CDeviceTSGetObservationResponse(double horizontalAngle, 
                                   double verticalAngle, 
                                   double slopeDistance)
       : CDeviceTSGetObservationResponse(CDeviceServerResponse::SUCCESS_MESSAGE,
                                         horizontalAngle,
                                         verticalAngle,
                                         slopeDistance) {}

   /**
    * Public constructor. Used to construct a response containing
    * the error message from the TotalStation device.
    *
    * @param errorMsg
    *           The error message for the response.
    */
   CDeviceTSGetObservationResponse(std::string errorMsg)
       : CDeviceTSGetObservationResponse(errorMsg,
                                         0.0,
                                         0.0,
                                         0.0) {}

private:
   /**
    * Private constructor.
    *
    * @param errorMsg
    *           The error message for the response.
    * @param horizontalAngle
    *           The horizontal angle.
    * @param verticalAngle
    *           The vertical angle.
    * @param slopeDistance
    *           The slope distance.
    */
   CDeviceTSGetObservationResponse(std::string errorMsg, 
                                   double      horizontalAngle, 
                                   double      verticalAngle, 
                                   double      slopeDistance) 
       : CDeviceServerResponse(CDeviceServerResponse::TS_GET_OBSERVATION, 
                               errorMsg),
         m_dHorizontalAngle(horizontalAngle), 
         m_dVerticalAngle(verticalAngle), 
         m_dSlopeDistance(slopeDistance){}

   /** The horizontal angle. */
   double m_dHorizontalAngle;

   /** The vertical angle. */
   double m_dVerticalAngle;

   /** The slope distance. */
   double m_dSlopeDistance;
}; // CDeviceTSGetObservationResponse

因此,如果没有问题,用户将调用传递三个布尔值的构造函数,基类上的错误消息将默认为成功。

So the user will either call the constructor passing three booleans if there was no problem, and the error message on the baseclass will default to success.

或者他们调用传递错误消息的构造函数,这将默认值为0.0。

Or they call the constructor passing in the error message, which will default the values to 0.0.

我认为我可以用上面的代码,但我得到以下错误信息:

I thought I could do this with the code above, but I get the following error message:

camd011> make
g++ -c CDeviceTSGetObservationResponse.cpp \
        -I.. -o bin/CDeviceTSGetObservationResponse.o
In file included from CDeviceTSGetObservationResponse.cpp:13:0:
CDeviceTSGetObservationResponse.h: In constructor 'device::response::CDeviceTSGetObservationResponse::CDeviceTSGetObservationResponse(double, double, double)':
CDeviceTSGetObservationResponse.h:44:10: error: type 'device::response::CDeviceTSGetObservationResponse' is not a direct base of 'device::response::CDeviceTSGetObservationResponse'
CDeviceTSGetObservationResponse.h:47:55: error: no matching function for call to 'device::response::CDeviceServerResponse::CDeviceServerResponse()'
CDeviceTSGetObservationResponse.h:47:55: note: candidates are:
CDeviceServerResponse.h:72:4: note: device::response::CDeviceServerResponse::CDeviceServerResponse(device::response::CDeviceServerResponse::EServerMessageIdentifier, std::string)
CDeviceServerResponse.h:72:4: note:   candidate expects 2 arguments, 0 provided
CDeviceServerResponse.h:31:7: note: device::response::CDeviceServerResponse::CDeviceServerResponse(const device::response::CDeviceServerResponse&)
CDeviceServerResponse.h:31:7: note:   candidate expects 1 argument, 0 provided
CDeviceTSGetObservationResponse.h: In constructor 'device::response::CDeviceTSGetObservationResponse::CDeviceTSGetObservationResponse(std::string)':
CDeviceTSGetObservationResponse.h:57:10: error: type 'device::response::CDeviceTSGetObservationResponse' is not a direct base of 'device::response::CDeviceTSGetObservationResponse'
CDeviceTSGetObservationResponse.h:57:42: error: 'errorMeg' was not declared in this scope
CDeviceTSGetObservationResponse.h:60:45: error: no matching function for call to 'device::response::CDeviceServerResponse::CDeviceServerResponse()'
CDeviceTSGetObservationResponse.h:60:45: note: candidates are:
CDeviceServerResponse.h:72:4: note: device::response::CDeviceServerResponse::CDeviceServerResponse(device::response::CDeviceServerResponse::EServerMessageIdentifier, std::string)
CDeviceServerResponse.h:72:4: note:   candidate expects 2 arguments, 0 provided
CDeviceServerResponse.h:31:7: note: device::response::CDeviceServerResponse::CDeviceServerResponse(const device::response::CDeviceServerResponse&)
CDeviceServerResponse.h:31:7: note:   candidate expects 1 argument, 0 provided
CDeviceTSGetObservationResponse.cpp: In member function 'void device::response::CDeviceTSGetObservationResponse::serialize(Archive&, unsigned int)':
CDeviceTSGetObservationResponse.cpp:38:14: error: 'base_object' is not a member of 'boost::serialization'
CDeviceTSGetObservationResponse.cpp:38:69: error: expected primary-expression before '>' token
make: *** [bin/CDeviceTSGetObservationResponse.o] Error 1


推荐答案

您正在寻找的是委派构造函数, ++ 11。

What you're looking for is delegating constructors and those are only available as of C++11.

例如:

struct A
{
    int x;

    A(): A(10) {}

private:    
    A(int val): x(val) {}
};

int main(int argc, char* argv[])
{
    A a;

    return 0;
}

正如你可以看到这里它与C ++ 11编译,但如果你看这里相同的代码不编译(先前到C ++ 11)

As you can see here it compiles with C++11, but if you look here the same code does not compile (prior to C++11)

这篇关于在同一个类中调用另一个构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆