gpt4 book ai didi

c++ - 使用数组的对象的编译时多态性是否可能?

转载 作者:太空狗 更新时间:2023-10-29 23:10:44 24 4
gpt4 key购买 nike

我还在学习,我有一个问题希望有人能帮助我:

是否可以在不使用虚函数的情况下实现从同一基类继承的不同类在数组中使用的编译时多态性?

任何对 C++ 有一点 OOP 了解的人,如果您有类似的东西

class Base {
public:
virtual void foo (){
std::cout << "I'm the base" << std::endl;
}
}

class A : public Base {
public:
void foo() {
std::cout << "I'm the child" << std::endl;
}
}

class B : public Base {
public:
void foo() {
std::cout << "I'm the child #2" << std::endl;
}
}


//Assume some main here

A *a = new A();
B *b = new B();

Base *someArray[] = {a,b};

someArray[0]->foo(); //"I'm the child"
someArray[1]->foo(); //"I'm the child #2"

我们知道这是可行的,因为函数和继承是在运行时解析的。但是我要知道的是如何在没有虚函数的情况下做到这一点,或者是否可以在不使用虚函数的情况下做同样的事情?

假设你做这样的事情

class Base {
public:
void foo (){
std::cout << "I'm the base" << std::endl;
}
}

class A : public Base {
public:
void foo() {
std::cout << "I'm the child" << std::endl;
}
}

class B : public Base {
public:
void foo() {
std::cout << "I'm the child #2" << std::endl;
}
}


//Assume some main here

A *a = new A();
B *b = new B();

Base *someArray[] = {a,b};

someArray[0]->foo(); //"I'm the base"
someArray[1]->foo(); //"I'm the base"

所以有很多选择,但我似乎无法找到与我想要的行为相同的东西。我读到的一种解决方案是制作一个像这样工作的静态类模板

template <class T>
class Base
{
public:
void interface()
{
// ...
static_cast<T*>(this)->implementation();
// ...
}

static void static_func()
{
// ...
T::static_sub_func();
// ...
}
};

class Derived : Base<Derived>
{
public:
void implementation() {
std::cout << "I am derived" << std::endl;
}
static void static_sub_func();
};

class AlsoDerived : Base<Derived>
{
public:
void implementation() {
std::cout << "I am also derived" << std::endl;

}
static void static_sub_func();
};


//Assume in some main

Derived div;
AlsoDerived alsoDiv;

Derived *someArray[] = { &div, &alsoDiv };//does not work not the same type
Base *someArray[] = { &div, &alsoDiv }; //does not work b/c its a template

以上不是我想要的行为。然后是看起来很有希望的静态转换,但是我需要知道在任何给定时间它会是什么类,这不是我想要的行为。

class Base {
public:
void foo (){
std::cout << "I'm the base" << std::endl;
}
}

class A : public Base {
public:
void foo() {
std::cout << "I'm the child" << std::endl;
}
}

class B : public Base {
public:
void foo() {
std::cout << "I'm the child #2" << std::endl;
}
}


//Assume some main here

A *a = new A();
A *b = new A();
B *c = new B();

Base *someArray[] = {a,b,c};

someArray[0]->foo(); //"I'm the base"
static_cast<A*>(someArray[1])->foo(); //"I'm the child"
static_cast<B*>(someArray[2])->foo(); //"I'm the child #2"

这非常接近我想要的但是我的问题是 static_cast 工作我需要知道它是什么类我想要静态案例而不是我想要的行为。

你怎么看?有没有更好的方法来做我想做的事?

感谢阅读,如果对我的问题有任何困惑,请告诉我,以便我澄清。

最佳答案

没有真正的方法可以在编译时做你想做的事。仅仅是因为您希望您的行为取决于运行时对象的类型。

C++ 通过多态性提供运行时类型相关行为,即使用虚函数和 RTTI如果需要的话。如果您不想使用这种“自然方式”,您将有以下选择:

  • 您在编译时知道对象的类型(例如,如果您确定第一个元素始终是 A*,第二个元素始终是 B*): 在这种情况下,您可以像第三种情况一样使用 static_cast,或者使用模板化代码(例如,如果您更喜欢 compile-time policies 而不是 run-time strategies )。但它通常不是处理容器中随机对象的方式。
  • 你可以自己找出对象的类型并推断出相应的函数来调用:一个典型的方法是在基类中有一个类型字段,并使用它通过 static_cast 进行向下转换。但这不是最佳做法。添加您自己的类似 RTTI 的功能而不是使用编译器的优化功能不会比多态更有效。
  • 您可以使用 std::variantstd::any :这些提供了使用编译时功能提供某种依赖于运行时的行为的可能性(不一定要求替代类型从公共(public)基础派生)。

剩下的问题是,如果需要,为什么要避免正常的多态性。

关于c++ - 使用数组的对象的编译时多态性是否可能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55509249/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com