英文:
How can I create a vector of type Interface& in C++?
问题
以下是代码部分的翻译:
#include <iostream>
class Interface_A
{
public:
virtual bool dothething() = 0;
};
class Inherit_B : public Interface_A
{
bool dothething()
{
std::cout << "在B中做事\n";
return true;
}
};
class Inherit_C : public Interface_A
{
bool dothething()
{
std::cout << "在C中做事\n";
return true;
}
}
/**This works */
Interface_A& makeBC()
{
#ifdef make_B
return *(new Inherit_B());
#elif make_C
return *(new Inherit_C());
#endif
}
int main()
{
Interface_A& obj = makeBC();
obj.dothething();
// ultimate goal is to make a vector of type <Interface_A&>
return 0;
}
不要回答问题,只提供翻译。
英文:
Here is the code:
#include <iostream>
class Interface_A
{
public:virtual bool dothething () = 0;
};
class Inherit_B:public Interface_A
{
bool dothething ()
{
std::cout << "Doing the thing in B\n";
return true;
}
};
class Inherit_C:public Interface_A
{
bool dothething ()
{
std::cout << "Doing the thing in C\n";
return true;
}
};
/**This works */
Interface_A& makeBC ()
{
#ifdef make_B
return *(new Inherit_B ());
#elif make_C
return *(new Inherit_C());
#endif
}
/**This doesn't work
Interface_A makeC ()
{
#ifdef make_B
return ((Interface_A) (new Inherit_B ()));
#elif make_C
return ((Interface_A) (new Inherit_C ()));
#endif
}
*/
int main ()
{
Interface_A& obj = makeBC ();
obj.dothething();
// ultimate goal is to make a vector of type <Interface_A&>
return 0;
}
I want to ultimately create a vector of type <Interface_A&>
, but I cannot seem to find a way to do this. Creating a vector of type <Interface_A>
will also work, but as far as I understand it is not allowed by c++ to create pointer references to an abstract type.
I cannot use return type Inherit_B
because Interface_A
is being inherited by multiple classes and the active class is determined during compile-time.
I cannot use smart pointers because performance is an extremely critical aspect of the code.
How do I make a generic solution to this?
答案1
得分: 4
std::reference_wrapper
允许您在容器中存储引用。然而,您并不需要引用。引用是指向对象的。您需要的是在某处存储对象并在向量中保留指向它的指针。这就是std::vector<std::unique_ptr<Interface>>
。
我不能使用智能指针,因为性能是代码极其重要的一个方面。
这是一个无关紧要的论点。当您决定使用继承和运行时多态性时,您已经引入了一层间接性。智能指针不会增加额外开销,但它们是您已经处于的情况的正确工具。
如果您想避免这种间接性,那么您应该重新考虑使用运行时多态性。
...在编译时确定活动类
这就是问题所在… 您并不需要运行时多态性。您的示例太模糊和抽象,无法建议更好的设计(模板?)。但是,当您不需要运行时多态性时,当然您也不想为此付出代价。
创建一个类型为<Interface_A>的向量也可以工作,但据我了解,C++不允许创建指向抽象类型的指针引用。
您可以使基类非抽象,但那么vector<Interface_A>
将不是正确的使用方式,因为对象切片的原因。请参阅 https://stackoverflow.com/questions/274626/what-is-object-slicing
英文:
std::reference_wrapper
lets you store references in container. However, you do not need references. References refer to objects. What you need is to store the object somewhere and keep a pointer to it in the vector. Thats a std::vector<std::unique_ptr<Interface>>
.
> I cannot use smart pointers because performance is an extremely critical aspect of the code.
Thats a moot point. You already bought in a level of indirection when you decided to use inheritance and runtime polymorphism. The smart pointers bring no additional overhead, but they are the right tool for the situation you are already in.
If you want to avoid that level of indirection then you should reconsider the use of runtime polymorphism.
> ... the active class is determined during compile-time
And there we go... you do not need runtime polymorphism. Your example is too vague and abstract to suggest a better design (templates?). But when you do not need runtime polymorphsim, then of course you do not want to pay for it.
> Creating a vector of type <Interface_A> will also work, but as far as I understand it is not allowed by c++ to create pointer references to an abstract type.
You could make the base class non-abstract, but then a vector<Interface_A>
will not be the right thing to use, due to object slicing. See https://stackoverflow.com/questions/274626/what-is-object-slicing
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论