#include <linux/input.h>
#include <string.h>
#include <gtest/gtest.h>
#include <string>
class Parent{
public:
Parent(){
}
virtual std::string GetString()
{
int amount = 1;
input_event ev[amount];
/**
* This solves the problem
*/
memset(ev, 0, sizeof(ev[0]));
ev[0].code = BTN_9;
ev[0].value = 1;
char* temp = reinterpret_cast<char*>(ev);
std::string msg(temp, sizeof(ev[0]) * amount);
return msg;
}
};
class Child : public Parent
{
public:
Child(){
}
virtual std::string GetString()
{
int amount = 1;
input_event ev[amount];
ev[0].code = BTN_9;
ev[0].value = 1;
char* temp = reinterpret_cast<char*>(ev);
std::string msg(temp, sizeof(ev[0]) * amount);
return msg;
}
};
class Child2 : public Parent
{
public:
Child2(){
}
virtual std::string GetString()
{
std::string temp(Parent::GetString());
return temp;
}
};
TEST(CastToString, test)
{
Parent parent = Parent();
Child child1 = Child();
Child2 child2 = Child2();
std::string pString(parent.GetString());
std::string c1String(child1.GetString());
std::string c2String(child2.GetString());
EXPECT_EQ(pString, c1String);
EXPECT_EQ(pString, c2String);
}
방금 전체 샘플을 복사했습니다. 문제는 Child2s GetString 함수를 호출 할 때 발생합니다. 항상 다른 값을 반환하므로 할당 문제가 있지만 가정 할 수 없습니다.부모의 가상 함수 호출
'ev' 정의 란 무엇입니까? –
가능한 경우 편집기 및 브라우저의 복사하여 붙여 넣기 기능을 사용해보십시오. 현재 양식의 코드는 컴파일되지 않습니다. – hvd
@hvd 왜 그런 말을합니까? – john