在某些情况下无法创建派生类的对象 [英] Failed to create object of derived class in some cases
问题描述
// Base Class
class Base
{
public:
void RegisterWithServer();
// some more functions
protected:
Base(std::string aTestName, std::string aServerName);
~Base();
private:
std::string mString;
std::string mServerName;
yami::agent mYAMIAgent;
};
// Base Class constructor
Base::Base(string aTargetName, string aServerName):
mString(aTargetName),
mServerName(aServerName)
{
// my debug statment
cout <<" we are in Base Class constructor"<< std::endl;
cout <<" in normal case we get this debug statement but in case of crash it doesnt reach till here I suppose "<< endl;
}
// DerivedClass
class derivedClient: public Base
{
public:
derivedClient(std::string aceptedString);
virtual ~derivedClient();
private:
std::string mServerAddress;
std::string mServerName;
};
// constructor
derivedClient::derivedClient(string aceptedString):
Base(aceptedString, "ServerTest"),
mServerAddress(aceptedString),
mServerName ("ServerTest")
{
cout <<" in normal case we get this debug statement but in case of crash it doesnt reach till here I suppose "<< endl;
RegisterWithServer(); // call base class function
};
// in some other function in different file
some function()
{
.....
.....
try
{
if( meet certain condition)
{
......
......
cout <<" the code reached till here" << std::endl;
derivedClient dClient("192.168.2.110");// problem ??
cout <<"in case of crash below lines are not printed nor any message is printed from both constructors "<< std::endl;
}
}
catch( const yami::yami_logic_error & aYamiException)
{
cout << "error: " << aYamiException.what() << endl;
}
}
b $ b我正在修复现有软件中的错误。
问题 - >在某些时候软件停止响应。
我做了什么 - >我已经跟踪的问题,发现当软件崩溃时,它失败,从对象创建之前从dervived类。
Hi Guys,
I am fixing the bug in the existing software .
Problem -> after some times the software stops responding .
What I have done -> I have traced the problem and found that when software crashes it fails before the object creation from dervived class.
派生类调用衍生构造函数和基础构造函数。我添加了调试语句,发现没有打印调试。
The derived class calls dervived constructor and base constructor. I have added debug statement and found nothing is printed on debug.
我想知道为什么代码甚至不会进入派生类和基类?是否有可能私人变量没有正确初始化?
I want to know why the code is not even going into derived class and base class ? is it possible that private variables have not been initialsed properly ?
请对示例代码进行一些评论。
感谢和问候,
Sam
Please throw some comments on sample code.
Thanks and regards,
Sam
推荐答案
我猜想 yami :: agent
的构造函数抛出一个异常, cout没有运行。正如Oli在注释中提到的,在输入类构造函数之前将构造一个类的所有成员变量,因此 yami :: agent :: agent()
将在 Base :: Base()
。
I'm would guess that the constructor for yami::agent
throws an exception which is why all the couts are not being run. As Oli mentioned in the comments all member variables of a class will be constructed before the class constructor is entered so yami::agent::agent()
will be run before Base::Base()
is entered.
编辑:
如果您有类似:
class MyObject
{
yami::agent m_Agent;
public:
MyObject::MyObject() { }
};
那么m_Agent将使用默认构造函数构造,它被声明为 agent const parameters& options = parameters());
。正如你所提到的,由于某些原因,yami :: agent :: agent()抛出一个异常,这可能是由于任何原因,如网络初始化错误,无效的输入参数等....
then m_Agent will get constructed using its default constructor which is declared as agent(const parameters & options = parameters());
. As you mention, for some reason yami::agent::agent() throws an exception which could due to any number of reasons such as a network initialization error, invalid input parameters, etc....
如果你想为yami :: agent构造函数指定自定义参数,你可以这样做:
If you want to specify custom parameters to the yami::agent constructor you can do something like:
yami::parameters& GetDefaultYamiOptions (void)
{
static yami::parameters options;
/* The following are just some example parameters */
options.set_integer(yami::option_names::connection_retries, 1);
options.set_integer(yami::option_names::connection_retry_delay_spread, 10);
options.set_integer(yami::option_names::tcp_nonblocking, true);
options.set_integer(yami::option_names::file_nonblocking, true);
options.set_integer(yami::option_names::tcp_connect_timeout, 1000);
return options;
}
class MyObject
{
yami::agent m_Agent;
public:
MyObject::MyObject() : m_Agent(GetDefaultYamiOptions())
{
}
};
您需要什么参数可能与我使用的参数不同,问题的原因可能不与不正确的参数相关。它很可能是来自操作系统或甚至硬件的低级网络错误。
What parameters you need may be different than what I use and the cause of the issue may not be even related to incorrect parameters. It could well be a low level networking error from the OS or even hardware.
这篇关于在某些情况下无法创建派生类的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!